Awk is a powerful text analysis tool, with the search for grep and the editing of SED, which is especially powerful when it comes to analyzing data and generating reports. To put it simply, awk reads the file line-by-row, using spaces as the default delimiter to slice each row, and then perform various analytical processing of the cut.
How to use
awk ' {pattern + action} ' {filenames}
where pattern represents what AWK looks for in the data, and the action is a series of commands that are executed when a match is found. Curly braces ({}) do not need to always appear in the program, but they are used to group a series of instructions according to a particular pattern. pattern is the regular expression to be represented, surrounded by slashes.
The most basic function of the awk language is to browse and extract information in a file or string based on the specified rules, before awk extracts the information for additional text operations. A complete awk script is typically used to format the information in a text file.
Typically, awk is treated as a unit of a file's behavior. awk processes the text by executing the corresponding command for each line that receives the file.
awk [-F field-separator] 'commands' input-file (s) where commands is the real awk command , [-F domain delimiter] is optional. input-file (s) is pending. In awk, each line in a file, separated by a domain delimiter, is called a domain. In general, the default field delimiter is a space without naming the-F domain delimiter.
Instance
Suppose the output of head-5/etc/passwd is as follows:
The/etc/passwd file holds the user's information, consisting of 6 semicolons of 7 information, as explained below
(1): User name.
(2): password (already encrypted)
(3): UID (User ID), operating system's own
(4): GID group identification.
(5): User's full name or local account
(6): Start directory
(7): The shell used to log in is the tool to parse the login command
[email protected] log]# head-5/etc/ passwdroot:x: 0:0: root:/root:/bin/bashbin:x:1:1: bin:/bin:/sbin/ NOLOGINDAEMON:X:2:2:d aemon:/sbin:/sbin/nologinadm:x:3:4 : adm:/var/adm:/sbin/nologinlp:x:4:7: lp:/var/spool /lpd:/sbin/nologin
If only the user name is displayed
[email protected] log]# head-5/etc/passwd | awk-f ': ' {print $} ' ROOTBINDAEMONADMLP
The awk workflow is this: reads a record with a ' \ n ' line break, then divides the record by the specified domain delimiter, fills the field, and $ $ represents all fields, representing the first field, $n representing the nth field. The default Domain delimiter is the "blank key" or "[tab] key", so the user name, $ = password
If you only display the/ETC/PASSWD account and the shell of the account, and the account and the shell are split by tab
[email protected] log]# head-5/etc/passwd | awk-f ': ' {print $ \ t ' $7} ' Root /bin/bashbin /sbin/nologindaemon /sbin/nologinadm /sbin/ NOLOGINLP /sbin/nologin
If you just show/etc/passwd's account and the shell of the account, and the account is separated by a comma from the shell, and the column name Name,shell is added to all rows, add "Admin,/bin/nobash" to the last line.
[[email protected] log]# head-5/etc/passwd | awk-f ': ' Begin{print ' Name,shell '} {print $ ', ' $7} END {print ' admin ,/bin/nobash "} 'name,shellroot,/bin/bashbin,/sbin/Nologindaemon,/sbin/ Nologinadm,/sbin/nologinlp,/sbin/nologinadmin,/bin/nobash
The awk workflow is done by first executing the beging, then reading the file, reading a record with the/n line break, and then dividing the record by the specified field delimiter, populating the field, and $ $ representing all fields, representing the first field, $n representing the nth field, The action action corresponding to the execution pattern is then started. Then start reading the second record ... Until all the records have been read, the end operation is performed.
Search all rows with the root keyword/etc/passwd
[email protected] log]# awk-f: '/root/'/etc/ passwdroot:x: 0:0: root:/root:/bin/bashoperator: x:0:operator :/root:/sbin/nologin
This is an example of the use of pattern, which matches the line of pattern (this is root) to execute the action (without specifying an action, the default output of the contents of each row).
Search support for the regular, for example, root start: awk-f: '/^root/'/etc/passwd
Search all lines that have the root keyword/etc/passwd and display the corresponding shell
[email protected] log]# awk-f: '/root/{print $7} '/etc/passwd/bin/bash/sbin/nologin
The awk command for Linux