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 for Shell programming (4)

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

In Linux system, there are three kinds of file sorting tools commonly used: sort, uniq, wc. This article will introduce the use of the first two tools.

Sort tool

Sort is a tool that sorts the contents of a file by behavior unit, and can also be sorted by different data types. For example, the game of data and characters is different.

The syntax of the sort command is

Sort [options] parameters Common option description-f ignores case-b ignores spaces in front of each line-M sorts by month-n sorts by number-r reverse sort-u is equivalent to uniq, indicating that homomorphic data displays only one line-t specifies the delimiter By default, use the [Tab] key to separate-o to save the sorted results to the specified file-k specified sorting area application example [root@localhost opt] # sort / etc/passwd / / sort the accounts in the / etc/passwd file abrt:x:173:173::/etc/abrt:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinapache:x:48:48:Apache:/usr/share / httpd:/sbin/nologinavahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologinchrony:x:995:991::/var/lib/chrony:/sbin/nologincolord:x:997:995:User for colord:/var/lib/colord:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologindbus:x:81:81:System message bus:/ : / sbin/nolog [root@localhost opt] # sort-t':'- rk 3 / etc/passwd / / reverse sort the third column in the / etc/passwd file nobody:x:99:99:Nobody:/:/sbin/nologinpolkitd:x:999:998:User for polkitd:/:/sbin/nologinlibstoragemgmt:x:998:996:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologincolord:x:997:995:User for colord:/var/lib/colord : / sbin/nologinsaslauth:x:996:76:Saslauthd user:/run/saslauthd:/sbin/nologinchrony:x:995:991::/var/lib/chrony:/sbin/nologingeoclue:x:994:989:User for geoclue:/var/lib/geoclue:/sbin/nologinsetroubleshoot:x:993:988::/var/lib/setroubleshoot:/sbin/nologinsssd:x:992:987:User for sssd:/:/sbin/nologingnome-initial-setup:x:991:986::/run/gnome-initial- Setup/:/sbin/nologinpostfix:x:89:89::/var/spool/postfix:/sbin/nologindbus:x:81:81:System message bus:/:/sbin/nologinmail:x:8:12:mail:/var/spool/mail:/sbin/nologinradvd:x:75:75:radvd user:/:/sbin/nologin// sorts the third column in the / etc/passwd file And save the output to the abc.txt file. [root@localhost opt] # sort-t':'- K3 / etc/passwd-o abc.txt [root@localhost opt] # cat abc.txt root:x:0:0:root:/root:/bin/bashsun:x:1000:1000:sun:/home/sun:/bin/bashqemu:x:107:107:qemu user:/:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologinusbmuxd:x:113:113:usbmuxd user:/:/sbin/nologinbin : x:1:1:bin:/bin:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinpulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologinrtkit:x:172:172:RealtimeKit:/proc:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinsystemd-network:x:192:192:systemd Network Management : /: / sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinnamed:x:25:25:Named:/var/named:/sbin/nologinrpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologinrpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologinuniq tool

The uniq tool is usually used in conjunction with the sort command in Linux systems to report or ignore duplicate lines in a file

Command syntax format

Uniq [options] parameters commonly used option description-c to count-d to show only duplicate lines-u to show only one line application example [root@localhost opt] # vim test.txt / / Edit test.txt test file Linux 10Linux 20Linux 30Linux 30Linux 30CentOS 6.5CentOS 6.5CentOS 6.5CentOS 7.3CentOS 7.3CentOS 7.3 ~ ~: wq / / Save exit [root@localhost opt] # uniq test.txt / / Delete the duplicate line Linux 10Linux 20Linux 30CentOS 6.5CentOS 7.3 in the testfile file [root@localhost opt] # uniq-c test.txt / / Delete duplicate lines in the test.txt file And show the number of repetitions of the line at the beginning of the line 1 Linux 10 1 Linux 20 3 Linux 30 3 CentOS 6.53 CentOS 7.3 [root@localhost opt] # uniq-d test.txt to find the duplicate line Linux 30CentOS 6.5CentOS 7.3 in the test.txt file

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