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 script programming (3) (awk, sort, uniq tools)

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

Share

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

Regular expressions of shell script programming (3) 1. Preface

The first two articles of ​ mainly introduce the concept and function of regular expressions, describe the concepts of basic regular expressions and extended regular expressions, and introduce the two commands grep and sed in shell's "three Musketeers". This article will introduce the last swordsman-awk.

​ at the same time, we will also introduce the sort tool and the uniq tool.

II. Awk tools

​ 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 output or filters the content that meets the conditions, 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

​ awk options' mode or condition {edit instruction} 'file 1 file 2

​ awk-f script file file 1 file 2

The ​ sed command is often used to process an entire line, while awk divides a line into multiple fields, and the field delimiter is a space or TAB key by default.

The execution result of ​ awk can print and display field data 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.

​ awk contains several special built-in variables (which cannot be modified but can be used directly)

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

​ NF: the number of fields in the currently processed row

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

​ $0: the contents of the currently processed line

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

​ FILENAME: name of the file being processed

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

Examples are described as follows:

1) find the user name, user ID, group ID column in the / etc/passwd file

[root@lokott ~] # awk-F:'{print $1 title 3 0bin 4}'/ etc/passwdroot 0 0bin 1 1daemon 2 2. Root@lokott / omit part of the content [root@lokott ~] # awk-F:'{print $0}'/ etc/passwd / / $0 shows the entire line The result is equivalent to cat and sed-n'p'. Root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin.// omits part of the content.

2) output text by line

[root@lokott ~] # awk'{print}'/ etc/passwd / / equivalent to awk'{print $0}'/ etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin / nologin [root@lokott ~] # awk 'NR==1 NR==3 {print}'/ etc/passwd / / outputs the line contents of 1-3 Equivalent to the following root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin [root@lokott] # 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