How to use Shell awk

Instructions

awk '{pattern + action}' {filenames}

Although operations can be complex, the syntax is always this, where pattern represents what AWK looks for in the data, and action is a sequence of commands to execute when a match is found. Curly braces ({}) do not need to appear all the time in a program, but they are used to group a sequence of instructions according to a specific pattern. pattern is the regular expression to be represented, enclosed in slashes.

The most basic function of awk language is to browse and extract information based on specified rules in files or strings. After awk extracts information, other text operations can be performed. Complete awk scripts are often used to format information in text files.

Usually, awk is a line processing unit of the file. awk receives each line of the file, and then executes the corresponding command to process the text.

call awk

1. Command line mode
awk [-F field-separator] 'commands' input-file(s)
Among them, commands are real awk commands, and [-F field separator] is optional. input-file(s) are the files to process.
In awk, on each line of the file, each item separated by a field separator is called a field. Normally, the default field separator is a space when the -F field separator is not specified.

[work@16-11-120 es-server]$ cat dd 
aaa bbb:ccc ddd eee fff:ggg
[work@16-11-120 es-server]$ cat dd |awk -F ':' '{print $1}'
aaa bbb
[work@16-11-120 es-server]$ cat dd |awk -F ':' '{print $1}' | awk '{print $2}'
bbb
[work@16-11-120 es-server]$ cat dd |awk '{print $1"\t"$3}'
aaa	ddd

The awk workflow is as follows: read in a record separated by '\n' newline character, then divide the record into fields according to the specified field separator, fill the fields, $0 means all fields, $1 means the first field, $ n represents the nth domain. The default domain separator is "space key" or "[tab] key", so $1 means logged in user, $3 means logged in user ip, and so on.

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325021523&siteId=291194637