Linux Shell programming awk and linuxshell programming awk
- Awk [-field-separator] 'commands' input-file (s)
Basic Mode
Use # to separate
Print all
- Awk '{print $1, $3}' a.txt
Print 1st and 3 columns
- Awk 'in in {print "0"} {print $1} 'a.txt
Print 0 on the first line
- Awk '{print $1} END {print "0"}' a.txt
Print the last line 0
- Awk '$1 = "0" {print $0}' a.txt
Rows with the first column equal to 0
- Awk '$1! = "0" {print $0} 'a.txt
Rows in the first column not equal to 0
Rows in the first column smaller than or equal to the second column
RegEx matches all rows whose columns do not contain 0.
- Awk '$1 ~ /(12 | (34)/'a.txt
RegEx matches the first column with 12 or 34 rows.
- Awk 'if ($1> 1 & $2 <1) {print $1} a.txt
The first column of the row where the first column is greater than 1 and the second column is smaller than 1
- Awk 'if ($1> 1 | $2 <1) {print $2} a.txt
The second column of the First or Second column that is less than 1
- Awk '{print nf rs nr}' a.txt
Number of columns of records, record separator, and number of read records are printed consecutively.
- Awk 'nr = FNR {print $1} NR> FNR {print $2} 'a.txt B .txt
Print the first column of the First file and the second column of the Second file
- Awk '{$1 = $1*2; print $1}' a.txt
Modify numeric print
- Awk 'in in {LAST = 0} {if ($1> LAST) print $1; LAST = $1 }'
Compare and print the incremental series one by one
- Awk '{total + = $1} END {print total}' a.txt
Statistical column Value
- Awk '{printf "% c", $1}' a.txt
Format output
- Awk '{print match ($1, "1")}' a.txt
Print the position of the first 1 In the first column without printing 0
- Awk '{gsub (/AB/, "cd", $1); print $0}' a.txt
String replacement in the first column
- Awk '{MAP [$1] =2 2} END {for (I in MAP) {print I, MAP [I]}' a.txt
Dictionary storage and Extraction