Linux awk command learning experience

  
 

awk is a powerful text analysis tool. Compared to grep search, sed editing, awk is particularly powerful when it analyzes data and generates reports. Simply put, awk reads the file line by line, and uses space as the default separator to slice each line. The cut part is then analyzed. Usage: awk '{pattern + action}' {filenames}

where pattern represents what AWK looks for in the data, enclosed in slashes, accepts regular expressions; and action is when a match is found A series of commands executed. Usually, awk is a unit of processing of a file. Each time awk receives a line of the file, it then executes the corresponding command to process the text.

Three ways to call awk:

Command line mode: awk [-F field-separator] 'commands' input-file(s)

where, commands are The real awk command, [-F field separator] is optional. Input-file(s) is the file to be processed. In awk, in each line of a file, each item separated by a domain separator is called a field. Typically, in the case of the unnamed -F domain separator, the default domain separator is a space. -F use method (by: for example): -F: or -F ':'

shell script mode:

Insert all awk commands into a file and make the awk program available Execute, then awk command interpreter as the first line of the script, called again by typing the script name. Equivalent to the first line of the shell script: #!/bin/sh, can be replaced with: #!/bin/awk

Insert all awk commands into a single file, then call: awk -f awk-script -file input-file(s)

While the -f option loads the awk script in awk-script-file, input-file(s) is the file to be processed. (Note the case of F)


Getting Started:

The awk workflow is like this: read a record with a '\ ' newline split , then divide the record by the specified domain separator, fill the domain, $0 for all domains, $1 for the first domain, $n for the nth domain, and the default domain separator for "blank keys" or " ;[tab] key".

If only the account showing /etc/passwd#cat /etc/passwd

Copyright © Windows knowledge All Rights Reserved