First, you need to first understand how awk works: 1. Awk reads one row of the input file at a time. 2. For each row, it matches in the given sequence mode. If it matches, the corresponding action is executed. 3. If no pattern match exists, any action is taken. 4. In the preceding syntax, either the search mode or action is optional, but cannot be the same. 5. If the search mode is not provided, then awk will perform the input action for each row. 6. If no action is given, print it. This is the default operation that matches all rows in the pattern. 7. No action brackets are left blank. It does not perform the default print operation. 8. The declaration of each action in should be separated by semicolons. Let us create the employee.txt file, which contains
First, you need to first understand how awk works:
1. Awk reads one row of the input file at a time.
2. For each row, it matches in the given sequence mode. If it matches, the corresponding action is executed.
3. If no pattern match exists, any action is taken.
4. In the preceding syntax, either the search mode or action is optional, but cannot be the same.
5. If the search mode is not provided, then awk will perform the input action for each row.
6. If no action is given, print it. This is the default operation that matches all rows in the pattern.
7. No action brackets are left blank. It does not perform the default print operation.
8. The declaration of each action in should be separated by semicolons.
Let us create the employee.txt file with the following content, which will be used
The example 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. Awk default behavior
By default, awk prints each row of a 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 above example, the mode is not given. Therefore, it applies to all rows.
By default, the entire line is output for the printed output of action and any parameter.
Example 2 of awk. Print the row that matches the pattern.
1234 $ awk '/Thomas/
>/Nisha/'employee.txt
100 Thomas manager sales $5,000
400 Nisha Manager Marketing $9,500
In the preceding example, it prints "Thomas" or "Nisha" to match all rows. It has two modes.
Example 3 of awk. Print only specific fields.
Awk has built-in variables. For each record, that is, row, separated by blank characters, the record is stored in $ n variables by default. If the row has four words, it is stored in $1, $2, $3, and $4. $0 indicates the entire row. NF is a built-in variable that represents the number of separated fields in this 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. initialize and final action
Awk two important pattern, identified by the keyword 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 'in in {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 preceding example, it prints the title of the report and the final file.
Example 6 of awk. Print the employee list of the technical department.
The Department name is now the fourth field, so check the string with "techology". 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
Operator ~ is a regular expression comparison. If the default operation is matched, the entire row is printed.
Example 7 of awk. Number of employees in the Technical Department
In the following example, check whether department is technology. If yes, only the variable count is added in the action. This variable is initialized to 0 in begin.
123456
$ Awk 'in in {COUNT = 0 ;}
$4 ~ /Technology/{count ++ ;}
End {print "Number of employees in technology dept =", count;} 'employee.txt
Number of employees in tehcnology dept = 3
Print this variable in the end, that is, the number of employees in the technology department.