Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell programming the awk of the three Musketeers

Shulou Source: shulou.com Published: 2022-06-03 04:37:38 10月05日 Update

Awk tool

In Linux/UNIX system, awk is a powerful editing tool, which reads input text line by line, searches according to the specified matching pattern, formats and outputs or filters the content that meets the requirements, and can achieve quite complex text operations without interaction. It is widely used in Shell scripts to complete a variety of automatic configuration tasks.

Common usage of awk

Typically, the command format used by awk is as follows, where single quotation marks and curly braces "{}" are used to set the processing action on the data. Awk can process the target file directly or through the "- f" read script.

Awk tends to divide a line into multiple "fields" before processing, and by default the field delimiter is a space or tab key. The result of awk execution can be printed and displayed through the function of print. In the process of using the awk command, you can use the logical operator "& &" for "and", "|" for "or", "!" It means "not"; you can also perform simple mathematical operations, such as +, -, *, /,%, ^ for addition, subtraction, multiplication, division, remainder, and multiplier, respectively.

Common formats of awk

Awk option 'mode or condition {edit instruction}' file 1 file 2 / / filter and output the contents of the file character condition

Awk-f script file 1 file 2 / / call edit instructions from the script, filter and output content

Awk-F':'{print $1 ID, group ID, etc. Columns such as user name, user ID, group ID, etc. / etc/passwd / / find out the user name, user ID, group ID, etc.

Awk reads information from an input file or standard input, and like sed, information is read line by line. The difference is that awk treats a line in a text file as a record and a part (column) of a line as a field (field) in a record. To manipulate these different fields, awk borrows a method similar to location variables in shell, using $1, $2, and $3 "to represent different fields in a row (record) sequentially. In addition, awk uses $0 to represent the entire row (record). Different fields are separated by specified characters. the default delimiter for awk is a space. Awk allows you to specify a delimiter in the form of a"-F delimiter "on the command line.

Awk contains several special built-in variables:

FS: specifies the field separator for each line of text, which defaults to spaces or tab stops.

NF: the number of fields in the rows currently being processed.

NR: the line number (ordinal) of the currently processed row.

$0: the entire line content of the currently processed row.

$n: the nth field (nth column) of the currently processed row.

FILENAME: the name of the file being processed.

RS: data records are separated. The default is\ n, that is, one record per behavior.

Example of awk usage:

1), output text by line

One to three lines of the awk 'NR==1,NR==3 {print}' / etc/passwd / / output / etc/passwd file

Awk-F ":" 'NR==1,NR==3 {print $1 recording 3}' / etc/passwd / / output / the first and third fields of one to three lines of the etc/passwd file

Awk-F ":"'(NR > = 1) & & (NR

Tags: Fields files output processing colons delimiters commands text differences content users scripts one line situation data conditions formatting spaces three lines statistics Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Linux macOS Shulou Tech Info OPPO Reno