Linux AWK Command

Linux Awk is a service that helps the user write very small yet effective programming codes mainly in the form of sentences or statements defining the text patterns. These patterns can be searched in each line of the code documentation and the user can take action when he finds a match within a statement. In this article, we will be discussing the awk command in Linux, the basic syntax for the awk command, creating a sample awk file, and several features of the awk command for a better understanding of the learner.

What is Linux awk command?

Linux Awk is basically a service that helps the user write very small yet effective programming codes mainly in the form of sentences or statements defining the text patterns. This command helps the users to easily work with functions, and operators such as logical operators, string functions, etc. The main function of using the awk command is to screen and then process. 

This command stands for A- Aho, W-Weinberger, and K- Kernighan. This was discovered in the 1970s by Bell Labs with the contribution of these 3 researchers and the name was kept on their names only. The awk is basically a shell script that is actually manipulative language. A user can use it to generate reports as well as for data manipulation. 

Want to know more about Linux, visit here Linux Training

Basic syntax for awk command?

The basic syntax of the awk command is :

awk options 'criteria_of_selection {action }' input_file > output_file

1. -f program-file: This command reads the program in awk directly from the program file and not from the command argument which is present in the first line.

2. -F fs: This command can be used as the input of the field separator. 

Let us understand this better with the help of a command example below:

$cat > employee1.txt

Output:

Garima sales_manager account 50000

Sonal finance_manager account 45000

Varya clerk account  15000

Tamanna peon manager 20000

Drishti peon 5000

Deepika Operations_manager account 25000

Sunita manager account 45000

Ram director account 80000
  • The default behaviour of the awk command is that the awk command prints each line of the data in the form of a specific line. There is no specific pattern for the same. 
$awk ‘{print}’  employee1.txt

Output:

Garima sales_manager account 50000

Sonal finance_manager account 45000

Varya clerk account  15000

Tamanna peon manager 20000

Drishti peon 5000

Deepika Operations_manager account 25000

Sunita manager account 45000

Ram director account 80000
  • To only print the files that match the given keywords:
$awk ‘/manager/ {print}’  employee1.txt

Output:

Garima sales_manager account 50000

Sonal finance_manager account 45000

Tamanna peon manager 20000

Deepika Operations_manager account 25000

Sunita manager account 45000
  • Awk command to split lines into fields: The mentioned print statement describes what is to be printed as the output. 
$ awk '{print $1,$4}' employee1.txt
Output:

Garima 50000

Sonal 45000

Varya  15000

Tamanna 20000

Drishti 5000

Deepika 25000

Sunita 45000

Ram 80000

Linux Certification Training

  • Master Your Craft
  • Lifetime LMS & Faculty Access
  • 24/7 online expert support
  • Real-world & Project Based Learning

Here, $1 represents the name of the employee and $4 represents the salary of the employee.

  • Use of NR variables: These variables keep a track of the current number of working employees and they are represented as lines. 

1. Below is an example of an awk command for using NF variables for representation as a field line.

$ awk '{print NR,$0}' employee1.txt

Output:

Garima sales_manager account 50000
Sonal finance_manager account 45000
Varya clerk account  15000
Tamanna peon manager 20000
Drishti peon 5000
Deepika Operations_manager account 25000
Sunita manager account 45000
Ram director account 80000

2. Below is an example of an awk command for using NF variables for representing the last field.

$ awk '{print $1, NF}' employee1.txt 

Output:

Garima 50000

Sonal 45000

Varya  15000

Tamanna 20000

Drishti 5000

Deepika 25000

Sunita 45000

Ram 80000

3.  Below is an example of an awk command for using NF variables for representing from line 2 to line 4.

$ awk 'NR==2, NR==4 {print NR,$0}' employee1.txt

Output:

2. Sonal finance_manager account 45000

3. Varya clerk account  15000

4. Tamanna peon manager 20000

4. Program using the awk command to print a line with a row number that is separated by ‘-’ from HKRTrainings.txt

$ awk '{print NR "- " $1 }' HKRTraining.txt

Output:

1- G

2- Sonal

3- Varya

4- Tamanna

Want to know more about Linux, visit here Linux Tutorial!

Subscribe to our youtube channel to get new updates..!

5.  Below is a program using the awk command to print the 2nd line from HKRTrainig.txt

$ awk '{print $2 }' HKRTraining.txt

Output:


Z

P22

B20

C41

6.  Below is a program to print nonempty line using the awk command

$ awk 'NF<0' HKRTraining.txt

Output:

0

7.   Below is a program to count the lines using the awk command

$ awk 'END{print NR}’ HKRTraining.txt

Output:

12

How does the awk command work?

The awk command works in such a way that it uses patterns that can be searched in each line of the code documentation and the user can take action when he finds a match within a statement. The awk command follows several operations to work. 

Step 1: The user needs to scan the line one by one.

Step 2: Then each line needs to be split into the form of fields.

Step 3: Then a comparison needs to be made between the input lines and the pattern fields.

Step 4: The action is then finally performed on the matched lines.

Sample File Creation using AWK Command

For creating a sample file using the awk command, the user needs to make use of the touch command. For an instance, touch textfile.txt. Here, textfile is the name of the user’s file. The comments can be added to the text file using the open command which is written as open textfile.txt. This command helps the text editor to open all the constituents of the file. 

This creates a file with the name of information.txt and it helps in separating the data in the form of distinguished columns.  The distinguished fields can look like the below format:capture2

The 5 columns in the above example are designed as first_name, last_name, age, place, and ID. The contents of this file can be seen by the user using the command cat textname_file and here textname_file is the file name. The content of the file is printed using the print $0  command and it worlds as cat ‘print($0)’  textname_file

In case, the user wants to print some specific columns, he can use the cat ‘print($1)’  textname_file command where $1 is the column number which the user wants to print. 

Features of awk command

There are a lot of features possessed by the awk command for making the work in Linux easier. Some of the features are listed below:

  • Awk command helps the user in scanning the code line by line.
  • It allows each line to be split into the form of fields.
  • It helps in the comparison made between the input lines and the pattern fields.
  • It takes the actions for finally performing on the matched lines.
  • It is easy to match the input file with the segment text using the awk command. 
  • The formatting of output lines is very easy.
  • A user can easily perform string and arithmetic operations using the awk command.
  • The awk command helps in performing arithmetic as well as string operations.
  • A user can simply produce format reports with the help of this command. 
  • The transformation of data is really quick based on a specific structure provided by the user.

Top 30 frequently asked Linux Interview Questions !

Linux Certification Training

Weekday / Weekend Batches

 Conclusion:

In this article, we have discussed the awk command in Linux and its uses. This command helps the users to easily work with functions, and operators such as logical operators, string functions, etc. The main function of using the awk command is to screen and then process. We have also discussed the basic syntax of this command using several programs for a better understanding of the learner. Then we talked about the features of the awk command and how a learner can work with it. 

Related Articles:

Find our upcoming Linux Certification Training Online Classes

  • Batch starts on 12th Aug 2022, Fast Track batch

  • Batch starts on 16th Aug 2022, Weekday batch

  • Batch starts on 20th Aug 2022, Weekend batch

Global Promotional Image
 

Categories

Request for more information

Saritha Reddy
Saritha Reddy
Research Analyst
A technical lead content writer in HKR Trainings with an expertise in delivering content on the market demanding technologies like Networking, Storage & Virtualization,Cyber Security & SIEM Tools, Server Administration, Operating System & Administration, IAM Tools, Cloud Computing, etc. She does a great job in creating wonderful content for the users and always keeps updated with the latest trends in the market. To know more information connect her on Linkedin, Twitter, and Facebook.

The awk command in a shell script is actually manipulative language. A user can use it to generate reports as well as for data manipulation. 

The awk command was discovered in the 1970s by Bell Labs.

The interpreter of the awk command is based on C language.

Both are very competitive languages, however, the main feature of awk that makes it better than python is that it is much faster than python.