[Introduction to awk: ]
awk can get some of the content from a single text, or format the text so that it is output in some way.
[awk Workflow: ]
awk will take the file line into memory and then segment the line (by default by Space or tab, $, $, $ ...). Then delete, then read the second line of content to memory ...Format: awk ' {/pattern/command1; command2;..} ' file # matches this line of the pattern to execute commands such as: Who | awk ' {print '} ' # prints the first chunk of the segment, which is the first paragraph, which is a row of everything
[awk parameter description: ]
-F Re: Allows awk to change its field delimiter . -V defines the variable, passing the variable from the shell to awk, such as-vdate= $DATE, passing the value of the variable in the shell to the awk variable DATE.
-F Progfile: Allows awk to invoke and execute the Progfile program file, of course Progfile must be a program file that conforms to the awk syntax.
[awk built-in variables: ]
ARGC Number of command-line arguments
ARGV command line parameter array
Argind the argv identifier of the file currently being processedawk ' {if (argind==1) {print $} if (argind==2) {print $}} ' aaa.txt Bbb.txt # First scan the AAA file and then scan the BBB file
NR number of records that have been readawk ' Nr==1,nr==5{print} ' Aaa.txt # shows 1 to 5 lines of aaa.txt files
FNR the number of records in the current fileawk ' Nr==fnr{print ' a '} NR > Fnr{print "B"} ' A.txt b.txt# Input file a.txt and B.txt, because the first scan a.txt, so scan a.txt when there must be NR==FNR;# Then when scanning b.txt, FNR starts counting from 1, while NR continues counting the number of rows A.txt, so nr > FNR
FS input field delimiter (default: space:), equivalent to-f optionawk-f ': ' {print $} ' ccc.txt # input file to: as a separator
OFS output field delimiter (default: space:)# output with; split①cat Ccc.txt as follows:
1:2:3
4:5:6② awk-f ': ' begin{ofs= '; "} {print $1,$2,$3} ' Ccc.txt
③ output results are as follows:
1;2;3
4;5;6
NF number of segments in the current recordawk-f ': ' {print NF} ' Ccc.txt
The RS input record delimiter, default to "\ n", by default, awk regards a row as a record; If RS is set, then awk splits the records by RS①cat Ccc.txt:hello World; I want to go swimming Tomorrow;hiahia
② run awk ' begin{RS = ";"} {print} ' Ccc.txt
③ results are as follows:Hello World
I want to go swimming tomorrow
Hiahia
ORS output record delimiter, default to line break, control output symbol after each print statement
awk ' begin{fs= ' \ n '; Rs= ""; Ors= ";"} {print NF} ' Ddd.txt
[Awk built-in functions: ]
Blength[([s])] calculates the string length (in bytes)
Length[([s])] calculates the string length (character)
RAND () generates random numbers
Srand ([expr]) set rand () seed
int (x) string converted to integral type
SUBSTR (S, M [, N]) substring
Index (s, t) position the first occurrence of the T string in the string s
Match (S, ere) matches regular ere,match in string s to modify Rstart, rlength variables.
Split (S, a[, FS]) splits a string into an array
Sub (ere, Repl [, in]) string substitution
Gsub Ibid.
sprintf (FMT, expr, ...) Spelling string
System (CMD) executes cmd in the shell.
ToUpper (s) string converted to uppercase
ToLower (s) string converted to lowercase
[awk Usage Example: ]
1.-F denotes what the delimiter awk-f: ' {print '} ' Ccc.txt # to: As a delimiter, print out the first paragraph in each line of the Ccc.txt filestring awk-f: ' {print $1,$2} ' ccc.txt # Prints the first and second strings, and if so, the printed content will be linked together
2./pattern/pattern matching awk-f: '/a/{print $ ' ccc.txt #to print out the first string containing a in the Ccc.txt file as a delimiter
3. ^ What starts withawk-f: '/^a/{print $ ' ccc.txt # to: As a delimiter, print out the first string in the Ccc.txt file that begins with a
4. ~ Indicates the meaning of the matchawk-f: ' $4 ~/a/{print} ' Ccc.txt # to: As a delimiter, print out the string containing a in the fourth paragraph of the ccc.txt file
5. Awk can do some other things before reading the first line into memory, using the Beginformat: awk ' Begin{command}/pattern/{command1; command2; ...} ' file
awk ' begin{fs= ': "}$1 ~/a/{print} ' Ccc.txt # before reading the Ccc.txt file, first execute the command in begin, set: as a separatorawk ' begin{fs= ': "; Ofs= "-"}$1 ~/a/{print $1,$2} ' ccc.txt # Output delimiter separated by-
6. After all the lines have been processed, awk can also start with the end format: awk ' Begin{command}/pattern/{command1; command2; ...} End{command} ' file
7. Count the number of segments after each row with NFWho | awk ' {print NF} '
8. Get awk to process the file contents in the source file is the first line in NRawk-f: '/^a/{print NR} ' Ccc.txt # Gets the string beginning with a in Ccc.txt is the first line
9. Custom Variablesawk ' begin{a=0}/tb/{a++} end{print A} ' Ccc.txt# before looping the Ccc.txt file, define a variable with an initial value of 0, and then loop through each row, giving a plus 1 for each row that contains a single line of TB, until the value of a is finally printed.