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 of shell scripts (3)-awk tool

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

Share

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

Awk tool

In Linux 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

Generally speaking, 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 {edit instruction}' file 1 file 2 "/ filter and output contents of file symbol condition awk-f script file 1 file 2" / / call edit instructions from the script to filter and output content

Awk relatively prefers to divide a line into multiple "fields" and then process them, 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.

/ etc/passwd is a very typical format file in Linux system, and the fields are separated by ":". Most of the log files in Linux system are also format files. Extracting relevant information from these files 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@192 ~] # awk-F':'{print $1 shutdownawk 3 shutdownawk 5} / etc/passwdroot 0 rootbin 1 bindaemon 2 daemonadm 3 admlp 4 lpsync 5 syncshutdown 6 shutdownawk

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 built-in variable (available directly) FS: specifies the field delimiter for each line of text, defaults to space or tab stop NF: the number of fields in the current processing line NR: the row number (ordinal) $0: the whole line content of the current processing line $n: the nth field (nth column) of the current processing line FILENAME: the file name processed RS: data record separation, the default is\ n, that is, one record per behavior awk usage 1. Output text by line awk'{print} 'try.txt / / output all content, equivalent to cat test.txtawk' {print $0} 'try.txt / / output all content, equivalent to cat test.txtawk' NR==1,NR==3 {print} 'try.txt / / output line 1' 3 content 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