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

Awk of shell three Musketeers (including sort sorting and uniq tools)

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

Share

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

Awk of the three Musketeers of shell

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.

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 tool command format awk options' mode or condition {edit instruction} 'file 1 file 2 "/ filter and output contents of file character condition awk-f script file 1 file 2" / / call editing instructions from the script, filter and output content 7

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.

Find the user name, user ID, group ID, and other columns of / etc/passwd, and execute the following awk command.

[root@localhost] # awk-F:'{print $1 title 3 awk 4}'/ etc/passwdroot 0 0bin 1 1daemon 2 2adm 3 4lp 4 7sync 5 0shutdown 6 0halt 7 0mail 8 12operator 11 0games 12 50nobody 99 99systemd-network 192 192dbus 81 81polkitd 999 998abrt 173173libstoragemgmt 996rpc 32 32colord 997 995saslauth 172 172pulse 171171chrony 995 991rpcuser 29 29nfsnobody 65534 65534ntp 38 38tss 59 59usbmuxd 113geoclue 994 989qemu 107radvd 75 75setroubleshoot 993 75setroubleshoot 992 988sssd 42 987gdm 74 987gdm 70 987gdm 89 987gdm 72 1000

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:

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

(2) NF: the number of fields in the rows currently processed.

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

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

(6) FILENAME: the name of the file being processed.

(7) RS9: data records are separated. The default is\ n, that is, one record for each behavior.

1. Awk usage (1) outputs all content, which is equivalent to cat chen.txt, [root@localhost ~] # awk'{print $0} 'chen.txt [root@localhost ~] # awk' {print} 'chen.txt / / this command has the same effect as the previous one # version=DEVEL#System authorization informationaulth-- enableshadow-- passalgo=sha512#Use CDROM installation mediacdlrom.thethethe. (2) output line 1 / 3 [root@localhost ~] # awk' NR==1 The command NR==3 {print} 'chen.txt / / has the same effect as the following command [root@localhost ~] # 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