Awesome Linux command line

To be honest with you, I like command line so much, even before I became familiar with Linux, I used to use DOS command prompt extensively, which in comparison with Linux command line is nothing.

After, I had experienced Linux for the first time, I have fascinated how it is powerful and how many tasks can be automated without writing a single line of code or bash script or doing it manually. For instance, many times I wanted to extract certain columns of a big text file without opening it and many other things.

This time I wanted to retrieve certain portion of text from a huge log file which contains some words and not extract the entire line but extract the content which has that particular word and the rest of the sentence.

For instance, I had the following sentence

“Hello world 1234, testing”

My objective is to extract “world 1234, testing” by checking for the word “world”. In order to do so, I have used the following command line,

less Mylog.txt | grep -o 'world.*' 

Now the next step is to remove the word “world” from the output in order to become only, “1234, testing”. For doing that I changed the command like below,

less Mylog.txt | grep -o 'world.*' | cut -f2- -d':' 

But the work has not done yet, I want to replace comma by new line to ensure each line has only one word. Therefore, the command extended like this

less Mylog.txt | grep -o 'world.*' | cut -f2- -d':' | tr -s ',' '\n' 

Now, the last step is to sort the content of the file and extract only the unique lines because some lines might be duplicated. And the last result become like this,

less Mylog.txt | grep -o 'world.*' | cut -f2- -d':' | tr -s ',' '\n' | sort | uniq 

You see how it is possible to do complicated things with Linux command line and save huge amount of time.

Leave a Reply