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.
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
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
$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
$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 '{print $1,$4}' employee1.txt
Output:
Garima 50000
Sonal 45000
Varya 15000
Tamanna 20000
Drishti 5000
Deepika 25000
Sunita 45000
Ram 80000
Explore Git Sample Resumes Download & Edit, Get Noticed by Top Employers!
Here, $1 represents the name of the employee and $4 represents the salary of the employee.
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!
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
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.
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:
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.
If you are Intrested learn OR know more about Linux One, visit here Liunx One training!
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:
Top 30 frequently asked Linux Interview Questions !
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:
Batch starts on 28th Sep 2023, Weekday batch
Batch starts on 2nd Oct 2023, Weekday batch
Batch starts on 6th Oct 2023, Fast Track batch
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.