1. Introduction
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, by default separating each row with a space delimiter , and then doing various analytical processing for the cut section.
2. 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
- After awk extracts the information, it can do other text operations. A complete awk script is typically used to format information in a text file
- Typically, awk is a unit of processing of a file, and awk handles the text with each line of the file it receives, and then executes the corresponding command.
3. Call awk
3.1. command-line mode
awk [-F Field-separator] ' commands ' input-file(s)
Where commands is the true awk command, [-F domain delimiter] is optional. Input-file (s) is the file to be processed.
In awk, each line of 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.
3.2. Insert all the awk commands into a separate file and then invoke:
awk awk-script-file input-file(s)
Where the-f option loads the awk script in Awk-script-file, Input-file (s) is the same as above.
4. Getting Started instance
requirement : Show only the/etc/passwd account and the shell of the account, and the account and shell are separated by commas, and add the column name Name,shell to all rows, add "Blue,/bin/nosh" to the last line.
#Cat /etc/passwd | awk ' : ' ' BEGIN {print "Name,shell"} {print $ "," $7} END {print "Blue,/bin/nosh"}'
Name,shell root,/bin/bash daemon,/bin/sh... . Blue,/bin/nosh
- Begin, then read into a record with A/n newline, and then divide the record into fields by the specified field delimiter.
- -f Specifies that the domain delimiter is ': ', the Default Domain delimiter is "blank key" or "[tab] key"
- Fills a field, which represents all fields, $ represents the first field, $n represents the nth field,
- The action that follows the execution pattern is {print $ "," $7}.
- Then start reading the second record ...
- Until all the records have been read, the end operation is performed.
requirement : Search all lines that have root keyword/etc/passwd and display the corresponding shell
awk ' /root/{print $7} ' /etc/passwd
/bin/bash
where Root in /root/ is the pattern of awk (matching the row containing root), representing the data to find. The action in awk is {print $7}
Linux command awk