There are certain commonly used commands for managing Linux Servers. Here are some useful grep, cut examples with unix pipelining for SSH. We wrote about UNIX Pipe before, the reader must read that article if he/she is not aware of using pipe. Theoretical knowledge and practical management of a server differs hugely. We often do not type cd
followed by space on Mac iTerm2, Homebrew Setup. We simply type /
and hit the return key. If I am at Desktop
on iTerm, and there is a directory named old-master
, I can omit cd and simply type old-master
and hit the return key. In bash shell there is an autocd
option. You can enable it by using shopt -s autocd
. It actually works on Ubuntu server. If you type /etc/php5/fpm/
(if exists), you’ll get -bash: /etc/php5/fpm/: Is a directory
as output on Ubuntu Server. After running shopt -s autocd
command, you can type /etc/php5/fpm/
to change directory. Let us comeback to our topic – grep, cut examples with unix pipe.
grep, cut Examples with unix pipe : Basics
Basically, we should show examples of grep
, then examples of cut
and then use the unix pipeline. As usually, we can run man grep
and man cut
to learn the basics about the utilities. The official definition is – “The cut utility cuts out selected portions of each line from each file and writes them to the standard output. If no file arguments are specified, or a file argument is a single dash, cut reads from the standard input.” For example, if we run this command :
1 | who | cut -c 1-16,26-38 |
The command who
would return output like this on my Mac :
---
1 2 3 | abhishekghosh console Dec 14 20:03 abhishekghosh ttys000 Dec 21 23:57 abhishekghosh ttys001 Dec 15 00:05 |
When I am piping it via cut
with arguments, I am getting this as output :
1 2 3 | abhishekghosh coc 14 20:03 abhishekghosh ttc 21 23:57 abhishekghosh ttc 15 00:05 |
Basically we need a data, ideally a text file for easy example of cut
. Let us wget Matt Cutts’ WordPress License :
1 | wget https://www.mattcutts.com/blog/license.txt |
This’ll extract only a desired column from the text file with -c option and displays the 2nd character from Matt Cutts’ WordPress License :
1 | cut -c2 license.txt |
It is quite bizarre output. You will get something like :
1 2 3 4 5 6 7 8 9 10 11 12 | h r o i u R h n h |
Maximum probably possible usage of cut
alone can be this example command extracts the fields in the file using a delimiter. The -d
option in cut utility can be used to specify the delimiter (' '
here) and -f
option is used to specify the field position :
1 | cut -d' ' -f2,3 -f1 license.txt |
You can compare :
1 | cut -d'/' -f2,3 -f1 license.txt |
The /download/source/
part from the url https://wordpress.org/download/source/
(last line in our case) will et vanished in our case.
Now, let see some examples of grep
. grep
prints lines of input matching a specified pattern. For example :
1 | grep "General" license.txt |
It will return a funny text with lines containing “General”. This is important as this command :
1 | curl -I https://thecustomizewindows.com |
returns a huge output, but we need to check whether the website is HSTS Preloaded or not. So, we can simple grep “Strict” :
1 | curl -I -s https://thecustomizewindows.com | grep "Strict" |
If you run :
1 | curl -I -s https://thecustomizewindows.com | grep "HTTP" |
It will return HTTP/1.1 200 OK
, but if domain is redirecting with 301, it will return only that :
1 | curl -I -s http://jima.in | grep "HTTP" |
grep, cut Examples with unix pipe : Combined
This was our basic goal to learn. grep will do some desired work, cut will filter the desired stuffs from that desired work. Unix Pipe will help. For example, this will return only the number of the HTTP status :
1 | curl -I -s https://thecustomizewindows.com | grep "HTTP" | cut -c10,11,12 |
Actually you can grep again :
1 | curl -I -s jima.in | grep "HTTP" | cut -c10,11,12 | grep "301" |
The above is a bogus way of using the utilities because we could grep "301"
before. That we shown for example only. Syntax Highlighting, however will colorize the texts. There are very powerful usages, depending on the need.