Categories: Unix Tutorials

Awk Command in Unix | What is Awk Command in Unix with Examples

Awk Command in Unix :

In my previous articles i have given the idea about different unix commands with its real life examples. In this article i would like to give you one of the important unix command which is Awk Command in Unix with its real life examples. Awk Command in Unix is most important command used to find and replace the text in unix. Awk Command in Unix is also used for database sorting and validating.I would like to give you the basic explanation of Awk Command in Unix with different real world examples so that user can understand the concept and the purpose of Awk Command.

Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan who has invented the AWK command

Awk Command in Unix Syntax and Usages :

The Awk Command in unix is mainly used for data manipulation with using file and generating the specified reports as well.The awk command programming language requires no compiling, and allows the user to use variables, numeric functions, string functions, and logical operators.The Awk Command in unix is just like a scripting language which is used for text processing.

Syntax :

awk ‘BEGIN {start_action} {action} END {stop_action}’ filename

As i already told you that Awk is used like scripting language we have different sections in Awk.

1-Begin-Section: 

The scripting will start here.

Start_action :

Here user can use any command which will be the starting action.

Action :

Here user can use set of commands to perform text manipulation.

Stop_action:

Here user can use command which will end the script.

Different Usages of Awk Command in Unix :

The Awk command is useful in following situations :

1.Scanning of the File Line by line.

2.It is used to transform the data files and data manipulation in text files.

3. Awk Command used to splits each input line into fields.

4. Awk Command compares input line/fields to pattern.

5. The main use of Awk command is to produce formatted reports.

6. The Awk command is used to perform the arithmetic and logical operations with ease.

7. The Awk command used to perform the conditional looping operations as well.

Options of Awk Command :

-F fs     : This option is used to specify a file separator.

-f file   : This option is used to specify a file that contains awk script.

-v var=value : This option is used to declare the variables.

These are above different options of Awk command. In next session we will check different real life examples of Awk Command.

Different Examples of Awk Command in Unix :

To check different examples let us create one file in unix using cat command.

$cat > employees.txt

Amit manager account 45000

Rohit clerk account 25000

Piyush manager sales 50000

Rahul manager account 47000

Sreenu peon sales 15000

Bikesh clerk sales 23000

Sreejit peon sales 13000

Aniket director purchase 80000

Example 1 : To look what is written in file.

The Print option of Awk command is used to check or print the contents in the File.

$Awk ‘{Print}’ employees.txt

The above command will print the content from the file named employees.txt.

Output :

Amit manager account 45000

Rohit clerk account 25000

Piyush manager sales 50000

Rahul manager account 47000

Sreenu peon sales 15000

Bikesh clerk sales 23000

Sreejit peon sales 13000

Aniket director purchase 80000

 

The print option of Awk command will print the file data on command prompt.

 

Example 2 :  Pattern Matching Technique in Awk.

The Awk command in unix is used to match the specified patterns. If user wants to search some pattern from given file then Awk command is very useful.

$Awk ‘/peon/ {Print}’ employees.txt

The Above Awk command will print the lines which has pattern ‘peon’ or who has ‘peon’ role.

Output :

Sreenu peon sales 15000

Sreejit peon sales 13000

Example 3 : Spliting Lines in to fields (Using Awk Build in variables )

The Awk command in unix will split the lines in to fields. You just need to use the dollar operator to specify the column of the line. Lets say in above examples you just want to know about the Employee,Position. You dont want to see the salary and designation. The Employee name is first line of the column.Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that break a line of text into individual words or pieces called fields.

Things to Remember in Awk Command in Unix (Built in variables)  :

  • $0 for the whole line.

  • $1 for the first field.

  • $2 for the second field.

  • $n for the nth field.

$Awk ‘{Print $1,$3}’ employees.txt

The above command will print the field no 1 as well as field no 3 together.

Output for the same is :

Amit account

Rohit account

Piyush sales

Rahul account

Sreenu sales

Bikesh sales

Sreejit sales

Aniket purchase

The above command will show only employee name and his/her department. I already explained that Awk command is very useful in reporting purpose. This option is very useful for generating the different kind of reports as it gives the data according to the field numbers.

Example 4 :  Printing Line number using NR variable in Awk Command

NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file.

$Awk ‘{Print NR ,$0}’ employees.txt

To print the number of lines with using Awk command user needs to use NR variable as well as $0 variable for Awk command. As we need to display or check the line number of each and every line of the file we need to use $0 variable of Awk command.

Output :

1 Amit manager account 45000

2 Rohit clerk account 25000

3 Piyush manager sales 50000

4 Rahul manager account 47000

5 Sreenu peon sales 15000

6 Bikesh clerk sales 23000

7 Sreejit peon sales 13000

8 Aniket director purchase 80000

Example 5 : Displaying First filed and Last field in reporting structure using NF variable

NF command keeps a count of the number of fields within the current input record.The NF command used to display last field in the given file. If user wants a report which contains the first field as well as last field then NF command is used.If user dont know the number of last field in the file then this command is always useful.

$Awk ‘{Print $1,$NF}’ employees.txt

The above command will give the output as follows :

Amit  45000

Rohit 25000

Piyush  50000

Rahul  47000

Sreenu  15000

Bikesh 23000

Sreejit 13000

Aniket 80000

Example 6 : If user wants to display specific lines  (Using NR command )

NR command is also useful in displaying the specific lines.

$Awk ‘NR == 2 , NR ==4 {Print NR ,$0}’ employees.txt

The NR == 2 indicates the starting point of the line (rowwise) and NR== 4 shows ending point of the line.So The above command will display the lines between 2 to 4.

Output :

2 Rohit clerk account 25000

3 Piyush manager sales 50000

4 Rahul manager account 47000

Example 7 : If you want to use seperator and print first line with line number.

User can add the seperator between line number and the column. Lets say user wants to use the Pipe as seperator.

$Awk ‘{Print NR ” | ” $1}’ employees.txt

The output of above file is :

1 | Amit

2 | Rohit

3 | Piyush

4 | Rahul

5 | Sreenu

6 | Bikesh

7 | Sreejit

8 | Aniket

Example 8 :  To check and print the longest line number of characters in the file

User can check the longest line and print it using Awk command. As i already told you the Awk uses the conditional formatting.Here to print the longest line from the file first user needs to check the count of words and letters rowwise and then display the longest line.

$ Awk ‘ { if (length($0) > max_len ) max_len = length ($0) } End { Print max_len } ‘ employees.txt

I would like to fragment the above command. The max_len variable includes the maximum length of the entire file and  if (length($0) > max_len ) this condition will check the maximum length line from the file and displays the line.

Output :

30

Example 9 : To count the lines in the file

The Awk command is used to count the lines in the file.

$ Awk ‘END { print NR } ‘ employees.txt

The above command will print the count of the number of lines.

The output for the same is :

8

Example 10 :  Print the lines with more than 28 characters.

If user wants to print the lines with some specified count of characters then Awk command is useful.

$ Awk ‘length($0) > 28 ‘ employees.txt

 

There is only a line which has more than 28 characters.

Output :

Aniket director purchase 80000

 

Example 11 :  Checking the cube of the given numbers.

User can write different programs using Awk command. I would like to give one example which will show the cube of given number till 3.

$ Awk ‘BEGIN { for(i=1,i<=3,i++) print “Cube of”,i ,”is”,i*i*i; }’

The above program will give the cube of the numbers 1 ,2, 3

Output :

Cube of 1 is 1

Cube of 2 is 8

Cube of 3 is 27

User can write these kind of for looping structure without use of any programming language in unix using Awk command.

Hope the above examples gives you best idea of Awk command in Unix. If you like this article or if you have any suggestions regarding the article of Awk Command in Unix kindly comment it in comment section.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

What is Root Cause Analysis (RCA) With real examples

In my previous article I have given details about application support engineer day to day…

1 month ago

Application Support Engineer Day to day responsibilities

In my previous articles I have given the roles and responsibilities of L1,L2 and L3…

1 month ago

What is mean by SLA ( Service Level Agreement) with Examples?

In my previous articles i have given the hierarchy of production support in real company…

1 month ago

What is Production support Hierarchy in organization?

In this article i would like to provide information about production support organization structure or…

1 month ago

What are roles and responsibilities for L3 Support Engineer?

In my previous article I have given roles for L1 and L2 support engineer with…

1 month ago

What are roles and responsibilities of L2 Engineer?

I have started this new series of how to become application support engineer. This article…

1 month ago