Awk
1. Domains and records:
Takes the first field and begins and ends with the character begin and end
You typically use begin to display variables and preset variables, using end to output the final result.
awk ' BEGIN {print ' being '} {print '} ' end {print ' End '} ' file
2. conditional operator:
The first field, which contains expect, prints the entire line
awk ' {if ($1~/expect/) print $} ' c.sh or awk ' $1~/expect/{print} ' file
Exact match: Print only records with 3 field equal to "48"
awk ' $3== ' {print $} ' file
Not equal to: awk ' $! = ' Asima ' temp
Mismatch: Print the entire record that does not contain Asima
awk ' $!~/asima/' Temp
Less than: awk ' {if ($1<$2) print $ "is smaller"} ' temp
Set case: awk '/[gg]reen/' temp prints a whole strip of records containing green, or green
Any character: awk ' ~/^...a/' temp prints the fourth character in the 1th field is a record, the symbol ' ^ '
Representing the beginning of the line, conforming to '. ' Represents any character
and relationship: awk ' {if ($1== "a" && $2== "B") print $} ' Temp
Or or relationship: awk ' {if ($1== "a" | | $1== "B") print $} ' Temp
For loop
Awk-f "," ' {for (i=1;i<=nf;i++) if ($i ~/a=/) {print $i}} '
3.awk built-in variables:
Example: awk ' end {print NR} ' temp Prints the number of read record strips at the end
awk ' {print nf,nr,$0} END {print FILENAME} ' temp
awk ' {if (nr>0 && $4~/brown/) print $} ' temp has at least one record and contains Brown
Another usage of NF: Echo $PWD | awk-f/' {print $NF} ' displays the current directory name
4. awk Operator:
Using operators in awk, basic expressions can be divided into numeric, string, variable, field, and array elements
Set input field to variable name:
awk ' {name=$1;six=$3; if (six== "man") the print name "is" six} ' temp
Domain Value comparison operation:
awk ' BEGIN {base= ' + '} {if ($4<base) print $} ' Temp
Modify numeric Field values: (the original input file will not be changed)
awk ' {if ($1== "Asima") $6=$6-1;print $1,$6,$7} ' temp
To modify a text field:
awk ' {if ($1== "Asima") ($1= "desc");p rint $ ' temp
Show only Modified records: (Show only what you need, differentiate the previous command, note {})
awk ' {if ($1== "Asima") {$1= "desc";p rint$1}} ' Temp
To create a new output domain:
awk ' {$4=$3-$2; print $4} ' temp
Statistic column values:
awk ' (tot+=$3); END {print tot} ' temp Displays the contents of each column
awk ' {(tot+=$3)}; End {print tot} ' temp shows only the final result
awk ' {total+=$1}end{print total} ' A.txt only shows the final result
Add File Lengths:
Ls-l|awk '/^[^d]/{print $8 "\ T" $ "{tot+=$5} end{print" TOTKB: "Tot} '
List file name only: Ls-l|awk ' {print $9} ' general case file name is domain 8th
5.awk printing all columns except the second column
Cat/etc/passwd|awk-f: ' {$2=null;print} ' is a column made empty
On the basic usage of awk