megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Find and remove duplicate lines using Regular Expressions

Open up your text editor and use the following RegEx pattern to find and remove the duplicate lines:

^(.*)(\r?\n\1)+$

I found this technique on Regular-Expressions.info and I'm going to quote their explanation over here:

The caret will match only at the start of a line. So the regex engine will only attempt to match the remainder of the regex there. The dot and star combination simply matches an entire line, whatever its contents, if any. The parentheses store the matched line into the first backreference.

Next we will match the line separator. I put the question mark into `\r?\n` to make this regex work with both Windows `(\r\n)` and UNIX `(\n)` text files. So up to this point we matched a line and the following line break.

Now we need to check if this combination is followed by a duplicate of that same line. We do this simply with `\1`. This is the first backreference which holds the line we matched. The backreference will match that very same text.

If the backreference fails to match, the regex match and the backreference are discarded, and the regex engine tries again at the start of the next line. If the backreference succeeds, the plus symbol in the regular expression will try to match additional copies of the line. Finally, the dollar symbol forces the regex engine to check if the text matched by the backreference is a complete line. We already know the text matched by the backreference is preceded by a line break (matched by `\r?\n`).

Therefore, we now check if it is also followed by a line break or if it is at the end of the file using the dollar sign.

The entire match becomes `line\nline` (or `line\nline\nline` etc.). Because we are doing a search and replace, the line, its duplicates, and the line breaks in between them, are all deleted from the file. Since we want to keep the original line, but not the duplicates, we use `\1` as the replacement text to put the original line back in.

Hope you found this tip useful!

Use node-sass to minify your CSS

If you want to turn your .scss files to minified .css files but without using Webpack or Gulp, just install the node-sass package using npm package manager and then run this on your terminal:

sass scss/style.scss css/style.css --style compressed

And now, you can use your style.css file on production! 😆

Hope you found this tip useful!

Create aliased class constants in Laravel

There are many ways to define constants in Laravel but I learnt a neat technique where you can define constants using an alias.

First off, create the following directory:

mkdir app/Constants

Next, create a file named MyConstants.php in the app/Constants directory and copy-paste the following code:

<?php
namespace App\Constants;

class MyConstants {
    const HELLO = 'hello';
}
?>

Then, go to the config/app.php file and define your new alias:

<?php
'aliases' => [
    // previously defined aliases...
    'MyConstants' => App\Constants\MyConstants::class,
]
?>

Lastly, execute the following commands to update your app's configuration:

php artisan config:clear
composer dump-autoload
php artisan config:cache

After that, you can use your new constants anywhere (Controllers, Models or Blades) like this:

<?php
echo MyConstants::HELLO;
?>

Learning this new technique helps me keep the code clean and makes it easier to trace the constants.

Hope you find this tip useful!

Rename a Git Branch

Using branches are one of the most powerful features of Git and becomes a part of the software development process.

Last night, I came across an issue where I created a new branch and committed my changes until the git tool rejected it because the branch was already created by someone else in the repository.

Luckily, I was able to resolve this issue by renaming my branch using git branch -m command.

Here's a short guide on how you can do that too!

1. Switch to the remote branch you want to rename

git checkout <your_old_branch>

2. Rename the current remote branch

git branch -m <your_new_branch>

Proceed to the next step, if you've pushed your old branch to the remote repository.

3. Push the renamed remote branch

git push origin -u <your_new_branch>

4. Delete the old remote branch

git push origin --delete <your_old_branch>

If you've come this far without any issues, you've successfully renamed your local and remote Git branch.

Hope you found this useful!

Import and Export MySQL database via Terminal

Sometimes, phpMyAdmin can be painful to use especially when you want to import/export a MySQL database.

If you're not afraid of using the Terminal, try these commands to save your time:

Import MySQL database

mysql -u username -p database_name < your_sql_file.sql

Before you run this command, please make sure that you've created the database_name schema in your database or else, you might get an error especially if the .sql file doesn't have a CREATE DATABASE statement.

Export MySQL database

mysqldump -u username -p database_name > your_sql_file.sql

This command will export your database with the file name your_sql_file.sql to your current path.

Hope this helps you out!

Concatenate multiple rows into one field

Say, you have a table named hobbies and wanted to display a list of hobbies based on user_id, you'd probably do something like this:

SELECT title FROM hobbies WHERE user_id = 8;

This would return a list of hobbies like this:

Boxing
Coding
Reading
Fishing

That's simple but what if you wanted to display them in one row? Like this:

Boxing, Coding, Reading, Fishing

You can make use of the GROUP_CONCAT method to achieve the same result by executing the following SQL query:

SELECT GROUP_CONCAT(title, SEPARATOR ', ') FROM hobbies WHERE user_id = 8;

Nice, what if you wanted to view a list of hobbies of all users? In most cases, a table like this might have a many-to-many relationship, so in order to avoid possible duplicates, you can try this:

SELECT user_id, GROUP_CONCAT(title, SEPARATOR ', ') FROM hobbies
GROUP BY user_id

Hope this tip helps you out!

Find your Public IP address using the Terminal

Previously, I used to determine my Public IP address on Google Search by typing "What is my IP?" and I was good with it.

Until, I thought of actually viewing it via the terminal itself. So, I wrote two lines of code in my ~/.bashrc file:

export myip="$(dig +short myip.opendns.com @resolver1.opendns.com)"
alias myip="echo $myip"

Alternatively, you could try this too:

export myip="$(dig TXT +short o-o.myaddr.l.google.com @ns1.google.com)"

Save the file and apply your new configuration by typing the following command:

source ~/.bashrc

That's it, now all you have to do is type myip in your terminal and it will display your Public IP address.

Hope you found this useful! 😀

Fix screen tearing in XFCE desktop environment

Yesterday, I decided to try XFCE desktop environment and boy, it's really faster than the GNOME desktop environment.

As soon as I started to play around with it, I noticed a good amount of screen tearing and to my surprise, I came to know that the XFCE environment is known to have such issues.

After a few minutes of research, I was able to fix it. Here are the steps:

Note: This is done on Ubuntu 18.04 Bionic Beaver

If you dont have the package inxi installed in your system, do it right now:

sudo apt install inxi

After you're done installing, type the following command to find out which graphics you're using:

inxi -G

If you're using an Intel Graphics Driver, you'll probably get something like this:

Graphics:  Card: Intel Device 5926
           Display Server: x11 (X.Org 1.19.6 ) driver: i915 Resolution: 1920x1080@60.00hz
           OpenGL: renderer: Mesa DRI Intel Iris Plus Graphics 640 (Kaby Lake GT3e) (KBL GT3)
           version: 4.6 Mesa 20.0.8

You can try go to Settings Manager->Window Manager Tweaks->Compositor" and enable **Synchronize drawing to the vertical blank. From what I've read, if you do that, it should stop but it didn't do anything for me.

If the above technique didn't work, go to /usr/shar/X11/xorg.conf.d/ and create a file for your graphics card named 10-intel.conf.

Copy-paste the following configuration into the file:

Section "Device"
  Identifier  "Intel Graphics"
  Driver      "intel"
  Option "TearFree" "true"
EndSection

Save the file, reboot your system and look for any screen tearing issues. If you didn't face any, that means it worked! 😁