After replacement by default, the replaced content is output. If you need to replace the original file directly, use-I:
When using double quotation marks, we can specify variables in the sed style and replacement string;
Awk data stream processing toolAwk script Structure
awk 'BEGIN{ statements } statements2 END{ statements }'
Work Mode
1. Execute the begin statement block;
2. Read a row from a file or stdin, and then execute statements2. repeat this process until all the files are read;
3. Execute the end statement block;
Print current row
When the print without parameters is used, the current row is printed;
echo -e "line1\nline2" | awk 'BEGIN{print "start"} {print } END{ print "End" }'
When print is separated by commas (,), the parameters are bounded by spaces;
echo | awk ' {var1 = "v1" ; var2 = "V2"; var3="v3"; \print var1, var2 , var3; }'___FCKpd___43gt;v1 V2 v3
Use-concatenation operator ("" As concatenation operator );
echo | awk ' {var1 = "v1" ; var2 = "V2"; var3="v3"; \print var1"-"var2"-"var3; }'___FCKpd___44gt;v1-V2-v3
Special variable: nr nf $0 $1 $2
NR: indicates the number of records, which corresponds to the current row number during execution;
NF: indicates the number of fields. The total number of fields corresponding to the current row during execution;
$0: The variable contains the text of the current row during execution;
$1: text content of the first field;
$2: Text Content of the second field;
echo -e "line1 f2 f3\n line2 \n line 3" | awk '{print NR":"$0"-"$1"-"$2}'
Print the second and third fields of each row:
awk '{print $2, $3}' file
Number of statistics files:
awk ' END {print NR}' file
Add the first field of each row:
echo -e "1\n 2\n 3\n 4\n" | awk 'BEGIN{num = 0 ; print "begin";} {sum += $1;} END {print "=="; print sum }'
Passing external variables
Var = 1000 echo | awk '{print vara}' vara = $ var # input from stdinawk '{print vara}' vara = $ var file # input from file
Filter the rows processed by awk using styles
Awk 'nr <5' # The row number is smaller than 5
Awk 'nr = 1, NR = 4 {print} 'file # print the row numbers equal to 1 and 4
Awk '/linux/' # lines containing linux text (can be specified using regular expressions, super powerful)
Awk '! /Linux/'# lines that do not contain linux text
Set the delimiter
Use-F to set the delimiter (space by default)
awk -F: '{print $NF}' /etc/passwd
READ command output
Use getline to read the output of the external shell command into the variable cmdout;
echo | awk '{"grep root /etc/passwd" | getline cmdout; print cmdout }'
Use loops in awk
for(i=0;i<10;i++){print $i;}for(i in array){print array[i];}
Print rows in reverse order: (implementation of the tac command)
seq 9| \awk '{lifo[NR] = $0; lno=NR} \END{ for(;lno>-1;lno--){print lifo[lno];}} '
Awk implements head and tail commands
head: awk 'NR< =10{print}' filename
tail: awk '{buffer[NR%10] = $0;} END{for(i=0;i<11;i++){ \ print buffer[i %10]} } ' filename
Print specified Column
Awk implementation:
ls -lrt | awk '{print $6}'
Cut implementation
ls -lrt | cut -f6
Print the specified text area
Determine the row number
seq 100| awk 'NR==4,NR==6{print}'
Confirm text
Print the text between start_pattern and end_pattern;
awk '/start_pattern/, /end_pattern/' filename
eg: seq 100 | awk '/13/,/15/'cat /etc/passwd| awk '/mai.*mail/,/news.*news/'
Common built-in functions of awk
Index (string, search_string): returns the position where search_string appears in string.
Sub (regex, replacement_str, string): Replace the first part of the regular expression with replacement_str;
Match (regex, string): checks whether regular expressions can match strings;
Length (string): returns the string length.
echo | awk '{"grep root /etc/passwd" | getline cmdout; print length(cmdout) }'
Printf is similar to printf in C language.
seq 10 | awk '{printf "->%4s\n", $1}'
Iterate the rows, words, and characters in the file
1. Each row in the iteration File
While Loop Method
While read line; doecho $ line; done <file.txt changed to sub-shell: cat file.txt | (while read line; do echo $ line; done)
Awk method:
cat file.txt| awk '{print}'
2. iterate every word in a row
for word in $line;do echo $word;done
3. iterate every character
$ {String: start_pos: num_of_chars}: extract a character from the string. (bash text slicing)
$ {# Word}: returns the length of the variable word.
for((i=0;i< ${#word};i++))doecho ${word:i:1);done
Original reprinted address: http://www.linuxprobe.com