1, the first column (field) of the print file: awk '{print $ 1}' filename
2, the first two columns of print files (domain): awk '{print $ 1, $ 2}' filename
3, printing the first column, and then print the second column: awk '{print $ 1 $ 2}' filename
4, the number of lines of printing text files: awk 'END {print NR}' filename
5, the first line of printed text: awk 'NR == 1 {print}' filename
6, the printed text of the second row, first column: sed -n "2, 1p" filename | awk 'print $ 1'
# (Get Line 4 test file)
#cat test | awk 'NR == 4' or cat test | sed -n '4p' direct access to a row of a
inside the shell of the assignment in two ways, the format
1) arg = `(command)`
2) arg = $ (command)
So, if you want to assign a certain number of lines in the file to a variable nlines, it can be expressed as:
1) nlines=`(awk 'END{print NR}' filename)`
or
2) nlines=$(awk 'END{print NR}' filename)
awk exercises
wang 4
which 3
zhao 4
liu 3
liu 3
chang 5
2
1 by the first field to find the length of 4 characters
Column 2 when the second value is greater than 3, create a blank file named line current domain $ 1 (touch $ 1)
Liu document 3 as the replacement string hong
4 and the second column seek
Second column averaging 5
Seeking a maximum value in the second column 6
7 After filtration repeated for each listed item, an occurrence number of each, the sum of the size of each of the first column
1, the string length
awk 'length($1)=="4"{print $1}'
2, execute system commands
awk '{if($2>3){system ("touch "$1)}}'
3, gsub (/ r /, "s", domain) with the specified domain (default $ 0) s alternative r (sed 's /// g')
awk '{gsub(/liu/,"hong",$1);print $0}' a.txt
4, column summation
df -h | awk '{a+=$2}END{print a}'
5, the column averaging
df -h | awk '{a+=$2}END{print a/NR}'
df -h | awk '{a+=$2;b++}END{print a,a/b}'
6, the column selecting the maximum value
df -h | awk 'BEGIN{a=0}{if($2>a) a=$2 }END{print a}'
7, the first column lists each filter repeated the number of times of occurrence of each one, the sum of the size of each
awk '{a[$1]++;b[$1]+=$2}END{for(i in a){print i,a[i],b[i]}}'