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

Regular expressions in shell programming (3) awk tool

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

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 option 'mode or condition {editing instruction}' file 1 file 2 "/ filter and output contents of file symbol condition awk-f script file 1 file 2" / / call editing instructions from the script to filter and output the content. As mentioned earlier, the sed command is often used to process an entire line, while awk tends to divide a line into multiple "fields" and then process it, and by default the field delimiter is a space or the 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.

/ etc/passwd is a very typical format file in Linux system. The fields are separated by ":". Most of the log files in Linux system are also format files. Relevant information is extracted from these files.

It is one of the daily work of operation and maintenance. If you need to find out the user name, user ID, group ID and other columns of / etc/passwd, execute the following awk command.

[root@localhost ~] # awk-F':'{print $1, 3 print 4}'/ etc/passwd

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 (which can be used directly) as follows:

FS: specifies the field delimiter for each line of text, which defaults to spaces or tab stops (tab). 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.

Usage example awk'{print} 'test.txt// outputs all content, which is equivalent to cat test.txtawk' {print $0} 'test.txt// output all content, equivalent to cat test.txt

Awk 'NR==1,NR==3 {print}' test.txt awk'(NR > = 1) & (NR

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