Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic usage of awk

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I would like to talk to you about the basic use of awk, many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Awk is an application that handles text files, and almost all Linux systems come with this program.

It processes each line of the file in turn and reads each field in it. Awk is probably the most convenient tool for text files with the same format per line, such as logs and CSV.

Awk is not only a tool software, but also a programming language. However, this article only introduces its command-line usage, which should be sufficient for most situations.

I. basic usage

The basic usage of awk is in the following form.

# format $awk action file name # example $awk'{print $0} 'demo.txt

In the above example, demo.txt is the text file to be processed by awk. There is a brace inside the front single quotation mark, inside which is the processing action print $0 for each line. Where print is the print command, and $0 represents the current line, so the result of the above command is to print each line as it is.

Next, let's demonstrate the above example with standard input (stdin).

$echo 'this is a test' | awk' {print $0} 'this is a test

In the above code, print $0 is the standard input this is a test and reprints it.

Awk divides each line into fields based on spaces and tabs, using $1, $2, $3 for the first field, the second field, the third field, and so on.

> $echo 'this is a test' | awk' {print $3}'a

In the above code, $3 represents the third field an of this is a test.

Next, for example, let's save the / etc/passwd file as demo.txt.

> root:x:0:0:root:/root:/usr/bin/zshdaemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologinbin:x:2:2:bin:/bin:/usr/sbin/nologinsys:x:3:3:sys:/dev:/usr/sbin/nologinsync:x:4:65534:sync:/bin:/bin/sync

The field delimiter for this file is a colon (:), so use the-F parameter to specify that the delimiter is a colon. Then its first field can be extracted.

Https://www.linuxprobe.com/basic-usage-awk.html

> $awk-F':'{print $1} 'demo.txtrootdaemonbinsyssync

Second, variables

In addition to the $+ number representing a field, awk provides a number of other variables.

The variable NF indicates how many fields there are in the current row, so $NF represents the last field.

> $echo 'this is a test' | awk' {print $NF} 'test

$(NF-1) represents the penultimate field.

> $awk-F':'{print $1, $(NF-1)} 'demo.txtroot / rootdaemon / usr/sbinbin / binsys / devsync / bin

In the above code, the comma in the print command indicates that the output is separated by a space between the two parts.

The variable NR indicates the number of lines currently being processed.

> $awk-F':'{print NR ")" $1} 'demo.txt root daemon bin sys sync

In the above code, in the print command, if you output characters as is, put them in double quotation marks.

Other built-in variables for awk are as follows.

FILENAME: current file name

FS: field delimiter, default is space and tab.

RS: line separator, which is used to split each line. The default is the newline character.

OFS: the delimiter of the output field, which is used to separate fields when printing. The default is a space.

ORS: the delimiter of the output record, which is used to print the time delimited record. The default is the newline character.

OFMT: format of digital output, default is% .6g

Third, function

Awk also provides some built-in functions to facilitate the processing of raw data.

The function toupper () is used to uppercase characters.

> $awk-F':'{print toupper ($1)} 'demo.txtROOTDAEMONBINSYSSYNC

In the above code, the first field is capitalized when it is output.

Other common functions are as follows.

Tolower (): the character is converted to lowercase.

Length (): returns the string length.

Substr (): returns a substring.

Sin (): sine.

Cos (): CoSine.

Sqrt (): square root.

Rand (): random number.

A complete list of awk built-in functions, you can see the manual.

IV. Conditions

Awk allows you to specify output conditions, outputting only rows that meet the criteria.

The output condition should be written in front of the action.

> $awk 'conditional Action' file name

Take a look at the example below.

> $awk-F':'/ usr/ {print $1} 'demo.txtrootdaemonbinsys

In the above code, the print command is preceded by a regular expression that outputs only lines that contain usr.

The following example outputs only odd lines and lines after the third line.

> # output odd lines $awk-F':'NR% 2 = = 1 {print $1} 'demo.txtrootbinsync# output lines after the third line $awk-F':'NR > 3 {print $1} 'demo.txtsyssync

The following example outputs a row where the first field is equal to the specified value.

> $awk-F':'$1 = = "root" {print $1} 'demo.txtroot$ awk-F':'$1 = = "root" | $1 = = "bin" {print $1} 'demo.txtrootbin

5. If statement

Awk provides an if structure for writing complex conditions.

> $awk-F':'{if ($1 > "m") print $1} 'demo.txtrootsyssync

The above code outputs a line where the first character of the first field is greater than m.

The if structure can also specify the else part.

> $awk-F':'{if ($1 > "m") print $1; else print "- -"} 'demo.txtroot-syssync after reading the above, do you have any further understanding of the basic use of awk? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report