Breaking News

Understand the basic concept of awk commands

awk command is a method that is used to process or analyze a text file that are organized by rows and columns. Or is a method that is used to search/find and replaces text.
When a searched text matches, then awk perform a specific action to that text or line. The program statement tells awk what action/operation to do.

The structure/format of the awk command is looked as:
awk 'pattern {action}' Input_File
The structure above means that, take each line of the Input_File, if the line contains the pattern apply the action to that line and write the resulting line to the Output_File. If the pattern is omitted, the action is applied to all line.
For Example: I have a file on my Desktop named as List that contains some Information of the passwd file, I want to use it to do some process using awk command. To start it, look on the following steps.
Note: This file is a normal file, so you can create your own and insert any information so as to do some practice.
  • Open your Linux Machine
  • Now Open terminal (This will be used to write awk commands)

  • Navigate to Desktop where your file is located by typing the following command and press enter.
  • cd /root/Desktop (This will change the current directory to Desktop)
  • Let us read on file called List and see what is inside on it. To do so, type the following command.
  • cat List (This will read a file called List)
  • Those informations is about users of my Computer (Server). Now let us type awk command to display only the name of users. To do so, type the following awk command.
  • awk -F: '{print $1}' List (This awk command, has the following meanings)
awk - This is a Syntax of using awk, so the Shell will interpret this command as awk command.
-F: - Stand for Field Separator. This is a switch/option that is used to identify the separator. As we said earlier in a definition that we process a file that is organized by rows and columns, so to identify between one column and another column we need a separator to separate them. By default a separator is a white space, but if your lines doesn't have a white space as separator, you have to tell awk command which separator you are going to use. In our file, the separator of the columns is ( : ) Look the last line start with SOC carefully and you will see it has seven columns.
'{print $1}' - This statement has two parts, First part ( print ) is a print statement that is used to display output to the screen. Second part ( $1 ) This ( $ ) represent the number of column to be printed out to the screen. So from our case, here we print out the first column of all rows.
List - This is the last part of our command which represent an Input _File (Read File). So, we process those information's that is obtained from List file
  • When we run that line of awk command the out put will be
  • Also, we can use piping ( | ) operator with awk command to perform a certain task. For example, we want to use ls -l command then with awk command to process its output. Let us see the list of files located in my current directory first. Type the following command and then press enter.
  • ls -l (This will show the lists of files located in current directory)
  • This list has nine columns separated by white space. Let us print the number of rows ( NR ), the second last column ( NF ) and write some text on output. Type the following awk command.
  • ls -l | awk '{print NR " = ", $(NF-1)}' (This awk command, has the following meanings)
NR - This stand for Number of Row. This will print the number of rows in a certain list. By default start with 1 to infinity, it depend on the number of rows.
" = " - This will be concatenated with the number of rows.
$(NF-1) - This will print the last second column. If the line has 10 columns, in this statement will print the ninth column. Simply, by using minus sign ( - ) it will count column from last to first.

Note: On the above example we used a piping operator ( | ) but we didn't discuss it, so if you don't have any idea about it, just click here to understand it.
  • Also we can use a condition with awk command. For example, from the list obtained with ls -l command, we want to print out the name of file(s) that contain number 2 in a second column.
  • Just understand, from which column you are going to put your condition and which column will be used for output. From our example, the second column will be use for condition (if this column is equal to number 2) and last column which contains a names of files will be used for output. Type the following command and press enter.
  • ls -l | awk '$2 == 2 {print $9}' (This will print out the name of files that contains number 2 in a second column)

  • Many actions/operations can be done with awk command and we can't finish each and everything here. I hope you have got some concept about it, if you will practice more with other commands which is different from these we have used, then you will be good.
  • ls -l | awk 'END { print NR }' (Use this awk command to get the total number of rows from listed list)
  • As you can see the function of awk command, it has a lot of functions but here we are giving you only a concept of it. You can learn more about it from different sources....!!
  • We are done....!!!
Please, comment and share with your friends.
Thank you...!!


  1. Well saiD,wonderful,understandable.....Next try to talk about sed and grep

  2. Replies
    1. Thank you so much!!! Please share with your friends...!!