Title: Use of the awk command
role:awk is a very good data processing tool that handles data in the fields of each row, with the default field separator being the SPACEBAR or the [tab] key
First, the basic structure of the awk script:
awk ' begin{print ' "start"} pattern {commands} End{print "ends"} file // An awk script typically consists of a BEGIN statement block, a generic statement block that can use pattern matching, and an end statement block 3 part, which is optional and can be used without any part of the script
//usually a sheet or double quotation mark surrounded by
For example: awk ' begin{i=0}{i++}end{print i} ' filename
awk "Begin{i=0}{i++}end{print i}" filename
Ii. the implementation process of awk
awk ' BEGIN {commands} patern{commands}end{commands} ' filename
[1] The first step: Execute the statement in the begin{commands} statement block;
[2] The second step: read a line from a file or standard input (stdin), and then execute the pattern{commands} statement block, which scans the file line by row, the first line to the last line to repeat the process until the file is fully read;
[3] Step three: Execute the end{commands} statement block when reading to the end of the input stream
The BEGIN statement block is executed before awk begins to read rows from the input stream, which is an optional block of statements. For example, the initialization of variables, the table-top statement of the printout table can usually be written in the BEGIN statement block.
The end statement block executes after awk reads all the rows from the input stream, such as the analysis results for all rows, such as the summary of information that is done in the end statement block.
Iii. instances of awk
Example 1:pay.txt file content format: name, first month salary, second month salary, third month salary, calculate the sum of March wages of each person
Name 1st 2nd 3th
Vbird 2300 3400 2500
Bmtsai 2000 2000 2300
BIRD2 4300 4200 4100
Command: awk ' nr==1{printf ("%10s%10s%10s%10s%10s\n", $ $, $ $, $ $, $4, "Total")} nr>=2{total=$2+$3+$4; printf ("%10s%10s%10s %10s%10s\n ", $, $, $ $, $4,total)} ' Pay.txt
Output:
Example 2: Add the first column of A.dat and the second column
A.dat content: B.dat content:
Command: awk ' begin{i=0;j=0}fnr==nr{array[i++]=$1;next}{total=array[j]+$1;print array[j],$1,total;j++} ' A.dat B.dat
Output Result:
Explanation: NR and Fnr are awk built-in variables that represent line numbers, nr represents the number of rows representing the records processed, and FNR represents the number of rows of files currently being processed. Can cause the next input line to be read and returned to the top of the script, which avoids other procedures for the current input line.
awk Common commands for Linux