Awk pattern scanning and processing language to process text and data.
Awk is a programming language used to process text and data under linux/unix. The data can come from stdin, one or more files, or the output of other commands. It is used on the command line, but more as a script. Awk has many built-in functions, such as arrays and functions, which is the same as C language. Flexibility is the biggest advantage of awk.
There is a detailed introduction to the principle of AWK in the link,
Here is a direct example:
(1) The first and second columns of the output file
Example file: log.txt
2 this is a test 3 Are you like awk This's a test 10 There are orange,apple,mongo
Command example:
$ awk '{print $1,$2}' log.txt 2 this 3 Are This's a 10 There
(2) Format output
$ awk '{printf "%-8s %-10s\n",$1,$4}' log.txt 2 a 3 like This's 10 orange,apple,mongo
(3) Use "," split
2,this is a test 3,Are you like awk 10,There are orange,apple,mongo
Command example:
$ awk -F, '{print $1,$2}' log.txt 2 this is a test 3 Are you like awk 10 There are orange
(4) Split using built-in variables
$ awk 'BEGIN{FS=","} {print $1,$2}' log.txt 2 this is a test 3 Are you like awk 10 There are orange
(5) awk -v # set variable
$ awk -F ',' -va=1 '{print $1,$1+a}' log.txt 2 3 3 4 10 11
(6) Using awk files
$ awk -f cal.awk log.txt
(7) Filter rows with the first column greater than 2
Sample file:
2 this is a test 3 Are you like awk 10 There are orange,apple,mongo 1 haha
$ awk '$1>2' log.txt 3 Are you like awk 10 There are orange,apple,mongo
(8) Filter rows with the first column equal to 2
$ awk '$1==2 {print $1,$3}' log.txt 2 is
(9) Filter rows whose first column is greater than 2 and whose second column is equal to 'Are'
$ awk '$1>2 && $2=="Are" {print $1,$2,$3}' log.txt 3 Are you
(10) Built in variables "FILENAME","ARGC","FNR","FS","NF","NR","OFS","ORS","RS"
$ awk 'BEGIN{printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n","FILENAME","ARGC","F NR","FS","NF","NR","OFS","ORS","RS";printf "------------------------------------ ---------\n"} {printf "%4s %4s %4s %4s %4s %4s %4s %4s %4s\n",FILENAME,ARGC,FNR, FS,NF,NR,OFS,ORS,RS}' log.txt FILENAME ARGC FNR FS NF NR OFS ORS RS --------------------------------------------- log.txt 2 1 5 1 log.txt 2 2 5 2 log.txt 2 3 4 3 log.txt 2 4 2 4
(11) Specifies the output delimiter
$ awk '{print $1,$2,$5}' OFS="=" log.txt 2=this=test 3=Are=awk 10=There= 1=haha=
(12) Use regular matching strings
The output second column contains "th" and prints the second and fourth columns
$ awk '$2 ~ /th/ {print $2,$4}' log.txt this a
(13) Ignore case
$ awk 'BEGIN{IGNORECASE=1} /this/' log.txt 2 this is a test
(14) Mode inversion
$ awk '$2 !~ /th/ {print $2,$4}' log.txt Are like There orange,apple,mongo haha
(15)awk output helloword
$ awk 'BEGIN { print "Hello, world!" }' Hello, world!
(16) Calculate file size
$ ls -l *.txt -rw-rw-r-- 1 sftcwl sftcwl 66 Nov 25 15:06 a.txt -rw-rw-r-- 1 sftcwl sftcwl 75 Nov 25 16:48 log.txt # sftcwl @ gz-cvm-ebuild-tongtian-dev001 in ~ [17:07:33] $ ls -l *.txt | awk '{sum+=$5} END{print sum}' 141
(17) Find lines longer than 80 from the file
awk 'length>80' log.txt
(18) The range pattern consists of two sets of characters separated by commas, starting with the record matching the first string and ending with the record matching the second string.
For example, display records from "Raptors" to "Celtics":
$ awk '/Raptors/,/Celtics/ {print $0}' teams.txt Raptors Toronto 58 24 0.707 76ers Philadelphia 51 31 0.622 Celtics Boston 49 33 0.598
(19) Range mode can also use relational expressions, for example, to display records with a fourth field equal to 31 to a fourth field equal to 34
[root@localhost ~]# awk '$4 == 31 , $4 == 34 {print $0}' teams.txt 76ers Philadelphia 51 31 0.622 Celtics Boston 49 33 0.598 Pacers Indiana 48 34 0.585
(20) When the file a.txt is known, the first column is the file name and the second column is the version number. Print a line with the maximum version number of each file. (awk required)
Copy code [root@w ~]# awk '{print}' a file 100 dir 11 file 100 dir 11 file 102 dir 112 file 120 dir 119 answer: [root@w ~]# awk '{if(code[$1]<$2) code[$1]=$2}END{for (i in code) print i,code[i] }' a file 120 dir 119
(21) count the sum of figures in a row
[root@chavinking mnt]# cat textfile chavinking 1 2 3 4 5 6 nope 1 2 3 4 5 6 [root@chavinking mnt]# cat textfile | awk '{for(i=1;i<=$NF+1;i++){sum=sum+$i} {print $1" "sum;sum=0}}' chavinking 21 nope 21 [root@chavinking mnt]#