The usage of awk print in shell

Time:2021-1-14

First, you need to understand how awk works
1。 Awk reads the input file one line at a time.
2。 For each row, it matches the pattern in the given order, and if it does, performs the corresponding action.
3。 If there is no pattern matching, any action will be performed.
4。 In the above syntax, either search mode or action is optional, but not at the same time.
5。 If no search pattern is given, then awk performs each line of input given action.
6。 If no action is given, print, which is the default operation for all lines that match the pattern. 7。 Any action that’s vacated doesn’t do anything. It does not perform the default print operation.
8。 Statements for each action in should be separated by semicolons. Let’s create employee.txt Documents, including

First, you need to understand how awk works

1。 Awk reads the input file one line at a time.
2。 For each row, it matches the pattern in the given order, and if it does, performs the corresponding action.
3。 If there is no pattern matching, any action will be performed.
4。 In the above syntax, either search mode or action is optional, but not at the same time.
5。 If no search pattern is given, then awk performs each line of input given action.
6。 If no action is given, print, which is the default operation for all lines that match the pattern.
7。 Any action that’s vacated doesn’t do anything. It does not perform the default print operation.
8。 Statements for each action in should be separated by semicolons.

Let’s create employee.txt File, which has the following content, which will be used in
Examples mentioned below.


$cat employee.txt
100 Thomas Manager Sales  $5,000
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA  Technology $6,000

Example 1 of awk. Default behavior of awk

By default, awk prints every line of the file.


1234567
$ awk '{print;}' employee.txt
100 Thomas Manager Sales  $5,000
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
400 Nisha Manager Marketing $9,500
500 Randy DBA  Technology $6,000

In the example above, the pattern is not given. Therefore, it is applicable to all lines.
Print output of action and any parameter. By default, the whole line is output

Example 2 of awk. Print lines that match the pattern.

1234$ awk ‘/Thomas/


> /Nisha/' employee.txt
100 Thomas Manager Sales  $5,000
400 Nisha Manager Marketing $9,500

In the example above, it prints all the lines that “Thomas” or “Nisha” match. It has two modes.

Example 3 of awk. Print only specific fields.

Awk has built-in variables. For each record, that is, a line, the delimited white space character delimits the record. By default, it is stored in $n variables. If the line has four words, it will be stored in $1,2, 2, 2, 3 and $4. $0 means the whole line. NF is a built-in variable that represents how many fields are separated in a row


12345678910111213$ awk '{print $2,$5;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000

$ awk '{print $2,$NF;}' employee.txt
Thomas $5,000
Jason $5,500
Sanjay $7,000
Nisha $9,500
Randy $6,000

Awk example 4. Initialization and final action

Two important patterns of awk are identified by the keywords begin and end

Syntax:

BEGIN { Actions}
{ACTION} # Action for everyline in a file
END { Actions }

#is for comments in Awk

This is an example

12345678910111213
$ awk ‘BEGIN {print “Name\tDesignation\tDepartment\tSalary”;}


> {print $2,"\t",$3,"\t",$4,"\t",$NF;}
> END{print "Report Generated\n--------------";
> }' employee.txt
Name Designation Department Salary
Thomas Manager Sales   $5,000
Jason Developer Technology $5,500
Sanjay Sysadmin Technology $7,000
Nisha Manager Marketing $9,500
Randy DBA  Technology $6,000
Report Generated
--------------

In the example above, it prints out the title of the report and the final document

Example 6 of awk. Print the employee list of the technology department.

Department name is now the fourth field, so you need to check the string with “technology”. If $4 matches, print this line
$ awk ‘$4 ~/Technology/’ employee.txt
200 Jason Developer Technology $5,500
300 Sanjay Sysadmin Technology $7,000
500 Randy DBA Technology $6,000

Operators are regular expression comparisons. If it matches the default operation, that is, print the entire line

Example 7 of awk. Print the number of employees in the technology department

In the following example, check whether the Department is technology. If it is, only the count of variables will be increased in action. This variable is initialized to 0 in begin

123456


$ awk 'BEGIN { count=0;}
$4 ~ /Technology/ { count++; }
END { print "Number of employees in Technology Dept =",count;}' employee.txt
Number of employees in Tehcnology Dept = 3

Print out this variable in end, which is the number of employees in technology department

summary

This article about the usage of awk print in shell is introduced here. For more details about the usage of awk print in shell, please search previous articles of developer or continue to browse the following articles. I hope you can support developer more in the future!