[awk Introduction:]
Awk can get part of the content from a single text, or typesetting the text so that it prints in some format.
[awk Workflow:]
Awk goes into memory with one line of the file, and then segments the line (by default, by Space or tab, $, $, $ ...). Then delete, and then read the second line of content to memory ...
Format: This line of awk ' {/pattern/command1; command2.} ' File # matching pattern executes the command
For example: Who | awk ' {print} ' # prints the first piece of the paragraph, which is the first paragraph, and all of the contents of $
[awk parameter Description:]
-F Re: Allows awk to change its field separator.
-V defines variables, passing variables from the shell to awk, such as-vdate= $DATE, passing the $date variable value in the shell to the awk variable DATE.
-F Progfile: Allows awk to invoke and execute Progfile Program files, of course Progfile must be a program file that conforms to the awk syntax.
[awk built-in variables:]
Number of ARGC command line arguments
ARGV array of command line arguments
Argind the argv identifier of the file currently being processed
awk ' {if (argind==1) {print} if (argind==2) {print $} ' aaa.txt bbb.txt # scan AAA file First, then scan BBB file
The number of records that NR has read out
awk ' Nr==1,nr==5{print} ' Aaa.txt # displays 1 to 5 lines of aaa.txt files
FNR the number of records in the current file
awk ' Nr==fnr{print "a"} NR > Fnr{print "B"} ' A.txt B.txt
# input Files A.txt and b.txt, because first scan a.txt, so scan a.txt must have NR==FNR;
# Then when scanning the b.txt, FNR starts counting from 1, while NR continues to count the number of a.txt lines, so nr > FNR
FS input Field separator (default: space:), equivalent to-f option
Awk-f ': ' {print} ' Ccc.txt # input file to: as a separator
OFS Output field separator (default: space:)
# when the output; Segmentation
①cat Ccc.txt are 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 current record
Awk-f ': ' {print NF} ' Ccc.txt
RS input Record separator, default to "\ n", by default, awk regards a row as a record; If RS is set, awk splits the records according to RS
①cat Ccc.txt:hello World; I want to go swimming Tomorrow;hiahia
② runs awk ' begin{RS = ';} {print} ' Ccc.txt
③ results are as follows:
Hello World
I want to go swimming tomorrow
Hiahia
ORS output record separator, 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])] to compute the string length (in bytes)
Length[([s])] calculates string length (character)
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/OS/Linux/
RAND () generates random numbers
Srand ([expr]) to set the rand () seed
int (x) string converted to integral type
SUBSTR (S, M [, n]) fetch substring
Index (S, T) locates the first occurrence of the T string in the string s
Match (S, ere) matches the regular ere,match in the string s to modify Rstart, rlength variables.
Split (S, a[, FS) splits a string into an array
Sub (ere, Repl [, in]) string substitution
Gsub ditto
sprintf (FMT, expr, ...) Scrabble 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 indicates what is used as the separator
Awk-f: ' {print '} ' Ccc.txt # to: As a separator, print out the first string in each row in the Ccc.txt file
Awk-f: ' {print $1,$2} ' Ccc.txt # Prints the first and second strings, so if you write $ $ then the printed content will be linked together
2./pattern/Pattern Matching
Awk-f: '/a/{print ' Ccc.txt # to: As a separator, print out Ccc.txt file contains a string of the first paragraph
3. ^ What is the beginning of the expression
Awk-f: '/^a/{print ' Ccc.txt # to: As a separator, print out the first segment of the Ccc.txt file with a beginning of a string
4. ~ Indicates the meaning of the match
Awk-f: ' $ ~/a/{print ' Ccc.txt # to: As a separator, print out a string containing a in the fourth paragraph of the Ccc.txt file
5. Awk can do other things before reading the first line into memory, using the BEGIN
Format: awk ' Begin{command}/pattern/{command1; command2;.} ' File
awk ' begin{fs= ': '}$1 ~/a/{print ' Ccc.txt # before reading Ccc.txt file, first executes the command in begin, setting: as a separator
awk ' begin{fs= ': "; Ofs= "-"}$1 ~/a/{print $1,$2} ' ccc.txt # Output delimiter separated by-
6. awk, after all the rows have been processed, can also take some action, using end
Format: awk ' Begin{command}/pattern/{command1; command2;} End{command} ' file
7. Count the number of segments after each row by NF
who | awk ' {print NF} '
8. Get awk Processing file content in the source file is the first few lines with NR
Awk-f: '/^a/{print NR} ' Ccc.txt # Gets a string beginning with a in Ccc.txt is the first line
9. Custom variables
awk ' begin{a=0}/tb/{a++} end{print A} ' Ccc.txt
# before looping ccc.txt files, define a variable with an initial value of 0; Each row is then cycled, and each row containing TB is given a plus 1, until the value of a is printed at the end.
Author: csdn Blog zdp072