Basic AWK command examples.

Along with SED command Linux – AWK is powerful command line tool which would help you to perform a lot of actions on your data. It can help you to sort, print and process your data in the way you like. You can use AWK to read and edit the file.

AWK is mostly used in the Shell Scripts. Lets check out Awk without wasting more time šŸ™‚

Let’s create a file called data.txt with the following contents – so we can learn AWK command better with examples.

Bob America Married 50
Jay India Unmarried 25
Abdul UAE married 33
Mona London Married 28
John America Unmarried 18
Neha India Unmarried 16

Run the command below which will print the contents in the file as it is.

[root@server ~]# awk '{print}' data.txt

Bob America Married 50
Jay India Unmarried 25
Abdul UAE married 33
Mona London Married 28

Print all the lines Matching with America.

[root@server ~]# awk '/America/ {print}' data.txt
Bob America Married 50
John America Unmarried 18

Only Print age from the file. As you can see that Age is written in the 4 column so we will use the command below.

[root@server ~]# awk '{print $4}' data.txt
50
25
33
28
18
16
[root@server ~]#

So now if you want to print Name and Age.

[root@server ~]# awk '{print $1, $4}' data.txt
Bob 50
Jay 25
Abdul 33
Mona 28
John 18
Neha 16

You can print age first by changing the order.


[root@server ~]# awk '{print $4, $1}' data.txt
50 Bob
25 Jay
33 Abdul
28 Mona
18 John
16 Neha
[root@server ~]#

Conditional Printing

I would like to print the names of the people who live in America. So here you will use If Condition in awk. In the example below it will check if the America is present in the column 2 it will print the column 1

[root@server ~]# awk '{ if ($2 == "America") print $1;}' data.txt
Bob
John

In the above example, if you just putĀ awk ‘{ if ($2 == “America”) print;}’ data.txt it will print the complete line.

AWK can do much more that what I have specified above. I will come up with Advanced AWK article soon.

Echo Command in Linux

Echo command mainly prints on the screen whatever you ask it. It a simple function but most the scripts would be incomplete with echo command. You won’t be able get visible output from the shell script, without echo command.

How to check what version of echo command you are using?

[root@server ~]# /bin/echo --version
echo (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Brian Fox and Chet Ramey.

How to print using echo command ?

it’s actually quite simple. Just use echo command following by the text.

[root@server ~]# echo "Hello JustGeek"
Hello JustGeek
[root@server ~]#

Printing Variables.

it’s not just for the print text there are many things you can do using echo command. Example you can print Variables.

[root@server ~]# IamVariable="This is JustGeek"
[root@server ~]# echo $IamVariable
This is JustGeek
[root@server ~]#

You can also run a command through echo command and print it’s output to the screen.

[root@server ~]# echo "This server is online from $(uptime | awk '{print $3,$4}')"
This server is online from 77 days,

Formatting using Echo Command.

To use formatting functions with echo -e. It enablesĀ interpretation of backslash escapes.
eg:- “\n” parameter will print on the new line. As shown below.

[root@server ~]# echo -e "First Line\nSecond Line"
First Line
Second Line
[root@server ~]#

There are multiple characters which can be used which suits you.

\a: Alert (historically known as BEL). This generates the default alert sound.
\b: Writes a backspace character.
\c: Abandons any further output.
\e: Writes an escape character.
\f: Writes a form feed character.
\n: Writes a new line.
\r: Writes a carriage return.
\t: Writes a horizontal tab.
\v: Writes a vertical tab.
\\: Writes a backslash character.

Grep Command Examples.

The grep is a Linux utility command which will help you in searching the contents within the file. It is the most useful when you are debugging through a large message file. It can be helpful in the scripts too.

Below are the few examples and the tricks for grep command.

Grep Command to highlight Color

Below command will search for “DB Error” in the /var/log/messages and highlight it with red color so you can quickly identify.

[root@server ~]# grep --color "DB Error" /var/log/messages

Print 3 lines before and after the pattern match in grep

If you want to print the lines before/after the pattern matches then you can use -A (after) or -B (before) option. Below command will print 3 lines before and after match.

[root@server ~]# grep "DB Error" /var/log/messages -A3 -B3

History Command Examples In Linux

To check history of the commands which has been executed.

[root@server ~]# history
    1  whoami
    2  clear
    3  ls
    4  whoaami
    5  ls -ltr
    6  top -c

To check last 5 commands in history use history 5

[root@server ~]# history 5
   92  clear
   93  whoami
   94  top -c
   95  history
   96  history 5

To clear history use history -c

[root@server ~]# history -c
[root@server ~]#
[root@server ~]# history
    1  history

To delete a particular command in history use history -d

[root@server ~]# history 5
1 history
2 whoami
3 clear
4 hisry 5
5 history 5

here I'm deleting the command which I fired at 4th Number
[root@server ~]# history -d 4
[root@server ~]# history
1 history
2 whoami
3 clear
4 history 5
5 history -d 4
6 history

To run a command at particular number in history you can use ! followed by the number. Example you want to run the command at number 2 the run !2

[root@server ~]# !2
whoami
root

TEE Command in Linux

TEE command is useful when you want to redirect the standard output to a file. Tee command should be already installed on your Linux Machine.

How to use Tee Command

[root@server ~]# cat file-1.txt | tee file-2.txt
a
b
c

This will overwrite all the contents of the file-2.txt with file-1.txt.
if want to keep the contents of the file-2.txt intact so you need -a option to append. As shown below.

[root@server ~]# cat file-1.txt | tee -a file-2.txt
one
word

Redirecting out of a command to a file. Most of us have habit of using >> to redirect output of a command to a file. But that’s work all the time where there is a error in the script.

So, to redirect output of a command to a file you need to use below command.
Example, my script has error but when I use >> to redirect output it won’t work.

[root@server scripts]# ./servicestatus.sh >> error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#

[root@server scripts]# ls -al error.log
-rw-r--r-- 1 root root 0 May 29 06:05 error.log
[root@server scripts]#

My error log file is blank, so here I will use TEE command which works perfect. It will re-direct stdout and stderr into a file.

[root@server scripts]# ./servicestatus.sh 2>&1 | tee error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#

[root@server scripts]# ls -al error.log
-rw-r--r-- 1 root root 66 May 29 06:08 error.log
[root@server scripts]#

[root@server scripts]# cat error.log
./servicestatus.sh: line 1: !/bin/bash: No such file or directory
[root@server scripts]#