The Linux awk command Detailed

awk
awk is a powerful text analysis tool, relative to grep to find, edit sed of, awk in its data analysis and report generation, is particularly strong. Awk is to simply read the file line by line, as the default delimiter spaces each slice row, and then the cut portion of a variety of analysis. awk is a line processor, the advantage compared to the screen processing, when dealing with large files do not appear out of memory or processing problem of slow, usually used to format the text information

awk process: sequentially processing each line, and then output

awk Usage:
awk parameter 'BEGIN {} // {action1; action2}' END {} file name

parameter: 

  • -F specify the delimiter
  • -f calling script
  • -v-defined variables

Begin {} initializer block, prior to processing each row, initialization code, mainly global variable is used, the separator is provided FS

// matching block may be a string or a regular expression

{} Command code block contains one or more commands, multiple commands; separated

END {} end of code blocks, each code block after the row processing is performed again, is mainly calculated final digest information or output end

Example: The total number of root line awk 'BEGIN {X = 0} / root / {X + = 1} END {print "I find", X, "root lines"}' / etc / passwd statistics / etc / passwd file contains

 

 

awk characters meaning:

  • $ 0 represents the entire current line
  • $ 1 per line first field
  • NF field variable number
  • NR record number of each line, multi-file recording increment
  • FNR and NR similar, but more documentation is not incremented each file from the beginning
  • \ T Tab
  • \ N newline
  • FS BEGIN defining delimiter
  • RS record delimiter input, default line break (i.e., line by line, text input is based)
  • ~ Contains
  • ! ~ Is not included
  • == equal, must be all equal, accurate comparison
  • ! = Not equal, accurate comparison
  • && Logical AND
  • || logical OR
  • + Represents at least one match, or a
  • / [0-9] [0-9] + / two or more digital
  • / [0-9] [0-9] * / or one or more digital
  • OFS output field separator, the default is the space, it can be changed to other
  • ORS output record separator, newline default, i.e., the processing result is output to the screen line by line
  • -F [: # /] defines three delimiter


print Print

  • print is the main command awk to print the specified content, you can also use printf
  • awk '{print}'  /etc/passwd   ==   awk '{print $0}'  /etc/passwd  
  • awk '{print ""}' / etc / passwd passwd content does not output, but the output of the same number of blank lines, and is further explained awk process text line by line
  • awk '{print "a"}' / etc / passwd a same number of output lines, only one line of a letter
  • awk -F:  '{print $1}'  /etc/passwd  ==   awk  -F  ":"  '{print $1}'  /etc/passwd
  • awk -F: '{print $ 1 $ 2}' input field 1, no intermediate partition
  • awk -F: '{print $ 1, $ 3, $ 6}' OFS = "\ t" / etc / passwd 1,3,6 output field, a tab as a delimiter
  • awk -F: '{print $ 1; print $ 2}' / etc / passwd input field 2, line breaks
  • awk -F: '{print $ 1 "**" print $ 2}' / etc / passwd input field 1, to the intermediate partition **
  • awk -F: '{print "name:" $ 1 "\ tid:" $ 3}' / etc / passwd custom format output fields 1,2
  • awk -F: '{print NF}' / etc / passwd each display line number field
  • awk -F: 'NF> 2 {print}' / etc / passwd number of lines per field is greater than the printed 2
  • Line 5 'NR == 5 {print}' / etc / passwd print out the / etc / passwd file: awk -F
  • awk -F: 'NR == 5 | NR == 6 {print}' / etc / passwd print out the / etc line 5 and line 6 / passwd file
  • awk -F: 'NR = 1 {print}!' / etc / passwd does not show the first line
  • awk -F: '{print> "1.txt"}' / etc / passwd file output to
  • awk -F: '{print}' / etc / passwd> 2.txt output to a file redirection                                     

Character matches

  • awk -F: '/ root / {print}' / etc / passwd file prints out lines contained in the root
  • OK '/' $ A '/ {print}' / etc / passwd file contains print out the variable $ A: awk -F
  • awk -F: '! / root / {print}' / etc / passwd file prints out lines do not have the root
  • awk -F: '/ root | tom / {print}' / etc / passwd file prints out lines contained in the root or tom
  • awk -F: '/ mail /, mysql / {print}' test print out the lines in the file containing the mail * mysql, * represents zero or any number of characters
  • awk -F: '/ ^ [2] [7] [7] * / {print}' test print out the file to the beginning of line 27, and the like as 27,277,27gff
  •  
  • awk -F: '$ 1 ~ / root / {print}' / etc / passwd file to print out the first line of field is the root
  • awk -F: '($ 1 == "root") {print}' / etc / passwd file to print out the first line of field is the root, equivalent to the above
  • awk -F: '! $ 1 ~ / root / {print}' / etc / passwd file to print out the first line of field is not the root
  • awk -F: '(! $ 1 = "root") {print}' / etc / passwd file to print out the first line of field is not the root, equivalent to the above
  • awk -F: '$ 1 ~ / root | ftp / {print}' / etc / passwd file print out the root or first field line ftp
  • awk -F: '($ 1 == "root" || $ 1 == "ftp") {print}' / etc / passwd file print out the first field or ftp root line, equivalent to the above
  • awk -F: '! $ 1 ~ / root | ftp / {print}' / etc / passwd file to print out the first line of field is not the root or not ftp
  • awk -F: '(!! $ 1 = "root" || $ 1 = "ftp") {print}' / etc / passwd file to print out the first line of field is not the root or not ftp, equivalent to the above
  • awk -F: '{if ($ 1 ~ / mail /) {print $ 1} else {print $ 2}}' / etc / passwd If the first field is mail, then print the first field, or print the second field


Formatted output
awk '{printf "% -5s% .2d", $ 1, $ 2}' test

  • printf output format representation
  • % Formatted output delimiter
  • -8 denotes a length of 8 characters
  • s represents a string type, d represents the decimal

For example:
1, the display / etc / passwd contained in the root line
awk '/ root /' / etc / passwd
2, to: Separated, display / etc / passwd and 7 in the first field of each line
awk -F ":" '{print $ 1 , $ 7}' / etc / passwd or awk 'the BEGIN {the FS = ":"} {Print $. 1, $. 7}' / etc / passwd
. 3, to: separated, display / etc / passwd in rows with the root of the first and seventh fields
awk -F ":" '/ root / {Print $ 1, $. 7}' / etc / passwd
. 4, to: separated, display / etc / passwd row beginning with root first and seventh fields
awk -F ":" '/ ^ the root / {Print $ 1, $. 7}' / etc / passwd
. 5, in order: separated, display / etc / passwd in the third field is greater than 999 the first row and the seventh field
awk -F ":" '$. 3> 999 {Print $ 1, $. 7}' / etc / passwd
. 6, to: separated, display / etc / passwd in the seventh field contains the bash the first row and the seventh field
awk -F ":" '$ ~. 7 "the bash" Print {$ 1, $}. 7' / etc / the passwd
. 7, in order:Separated, display / etc / passwd in the seventh field does not contain bash the first and seventh columns of the row
awk -F ":" '! $ 7 ~ "nologin" {print $ 1, $ 7}' / etc / passwd
8,: is separated, the display of the first and seventh fields $ 3> 999, and a seventh field contains bash row
awk -F ":" '$ 3 > 999 && $ 7 ~ "bash" {print $ 1, $ 7}' / etc / the passwd
. 9, in order: as a separator, the display of the first and seventh fields $ 3> 999 or seventh field contains bash row
awk -F ":" '$ 3 > 999 || $ 7 ~ "bash" { print $ 1, $ 7} ' / etc / passwd

Guess you like

Origin www.cnblogs.com/sx66/p/11892274.html