Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use awk, sort and uniq in regular expressions of Shell script

Shulou Source: shulou.com Published: 2022-06-01 03:00:15 11月06日 Update

This article mainly shows you the "Shell script regular expressions in awk, sort, uniq how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Shell script regular expressions in awk, sort, uniq how to use" this article.

Awk tool

In the Linux/UNIX system, awk is a powerful editing tool, which reads the input text line by line, searches according to the specified matching pattern, formats and outputs or filters the content that meets the criteria, 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 uses of 1.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.

Demo1:

Find out the user name and other columns of / etc/passwd and execute the following command

[root@localhost ~] # awk-F':'{print $1 1daemon 4} / etc/passwdroot 0 0bin 1 1daemon 22 awk / the following omitted multiple lines contains several special built-in variables (available directly) as follows: FS: specifies the field delimiter for each line of text, which defaults to spaces or tabs. 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.

Output text by line:

[root@localhost ~] # awk 'NR==1,NR==3 {print}' / etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin [root@localhost ~] # awk-F ":" 'NR==1,NR==3 {print $1 awk 3}' / etc/passwdroot 0bin 1daemon 2 [root@localhost ~] # awk-F ":'(NR > = 1) & & (NR)

Tags: Fields processing output sorting content commands files tools scripts data text statistics users regular expressions colons commonly used formatting articles systems Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno vpn Xiaomi macOS