Sort Tool The Linux sort command is used to sort the contents of text files. sort common options b Ignore the space characters at the beginning of each line. sort tool example Default By default, the sort tool sorts in alphabetical order. [root@1centos ~]# sort /etc/passwd abrt:x:173:173::/etc/abrt:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin chrony:x:992:987::/var/lib/chrony:/sbin/nologin cockpit-ws:x:990:984:User for cockpit-ws:/:/sbin/nologin colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin dirsrv:x:988:982:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin dovenull:x:981:975:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin Sort /etc/passwd in reverse order by the third column Here is the order in reverse order by numbers. [root@1centos ~]# sort -t: -rk 3 /etc/passwd nobody:x:99:99:Nobody:/:/sbin/nologin ods:x:999:999:softhsm private keys owner:/var/lib/softhsm:/sbin/nologin polkitd:x:998:997:User for polkitd:/:/sbin/nologin colord:x:997:994:User for colord:/var/lib/colord:/sbin/nologin unbound:x:996:993:Unbound DNS resolver:/etc/unbound:/sbin/nologin gluster:x:995:992:GlusterFS daemons:/run/gluster:/sbin/nologin libstoragemgmt:x:994:991:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin saslauth:x:993:76:Saslauthd user:/run/saslauthd:/sbin/nologin chrony:x:992:987::/var/lib/chrony:/sbin/nologin geoclue:x:991:985:User for geoclue:/var/lib/geoclue:/sbin/nologin cockpit-ws:x:990:984:User for cockpit-ws:/:/sbin/nologin sssd:x:989:983:User for sssd:/:/sbin/nologin dirsrv:x:988:982:user for 389-ds-base:/usr/share/dirsrv:/sbin/nologin setroubleshoot:x:987:981::/var/lib/setroubleshoot:/sbin/nologin saned:x:986:980:SANE scanner daemon user:/usr/share/sane:/sbin/nologin gnome-initial-setup:x:985:979::/run/gnome-initial-setup/:/sbin/nologin pcp:x:984:978:Performance Co-Pilot:/var/lib/pcp:/sbin/nologin kdcproxy:x:983:977:IPA KDC Proxy User:/:/sbin/nologin ipaapi:x:982:976:IPA Framework User:/:/sbin/nologin dovenull:x:981:975:Dovecot's unauthorized user:/usr/libexec/dovecot:/sbin/nologin dovecot:x:97:97:Dovecot IMAP server:/usr/libexec/dovecot:/sbin/nologin hsqldb:x:96:96::/var/lib/hsqldb:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologi ...omitted... Sort the third column of /etc/passwd and output it to px.txt [root@1centos ~]# sort -t: -k 3 /etc/passwd -o px.txt [root@1centos ~]# cat px.txt root:x:0:0:root:/root:/bin/bash xnftp:x:1007:1007::/home/xnftp:/sbin/nologin vuser:x:1008:1008::/opt/vuser:/sbin/nologin tom:x:1009:1009::/home/tom:/bin/bash jerry:x:1010:1010::/home/jerry:/bin/bash kongkong:x:1011:1011::/home/kongkong:/bin/bash qemu:x:107:107:qemu user:/:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin uniq tool The Linux uniq command is used to check and delete repeated lines and columns in text files, and is generally used in conjunction with the sort command. uniq common options uniq can check for repeated lines and columns in a text file. uniq Tool Examples View the file the.txt [root@1centos zhengzebiaodashi]# cat the.txt 1the 2the 3the 1the 2the 3the 1the 2the 3the 1the 2the 3the 1the 2the 3the 2the 2the 3the 3the 2the 3the 4the 5the 4hello hi the word world 5 2 3 4 5 6 7 8 Default sort [root@1centos zhengzebiaodashi]# uniq the.txt 1the 2the 3the 1the 2the 3the 2the 2the 3the 3the 2the 3the 4the 5the 4hello hi the word world 5 2 3 4 5 6 7 8 Delete duplicate rows and display the number of occurrences [root@1centos zhengzebiaodashi]# uniq -c the.txt 4 1the 2the 3the 2 1 1the 2the 3the 1 2the 2the 3the 1 3 the 2 the 3 the 4 the 5 the 1 4hello hi the word world 1 5 2 3 4 5 6 7 8 Find duplicate lines in the testfile file [root@1centos zhengzebiaodashi]# uniq -d the.txt 1the 2the 3the tr tool Tr is the abbreviation of translate, which is used for translation or conversion. Specifically, it can transform or delete the input content (stdin). It is an essential tool for Linux pipelines. Here are some common usages tr common options -c: replace all characters that do not belong to the first character set; tr tool example with echo to uppercase lowercase letters [root@1centos zhengzebiaodashi]# echo "jb51" |tr 'az' 'AZ' JB51 Replace repeated characters in output [root@1centos zhengzebiaodashi]# echo 'Thisssssss is cdsnnn' |tr -s 'sn' This is cdsn Delete silent characters in a string [root@1centos zhengzebiaodashi]# echo 'this is csdn' |tr -d 'th' is is csdn Sorting an Array With these tools, you can easily sort the array in ascending or descending order. #!/bin/bash read -p "Please enter your array, separated by spaces:" a shuzu=($a) echo "Your array is: ${shuzu[*]}" echo "Array in ascending order is:" echo "$a" |tr ' ' '\n' |sort -n |tr '\n' ' ' echo '' echo "Array in descending order is:" echo "$a" |tr ' ' '\n' |sort -nr |tr '\n' ' ' echo " " Use [root@1centos zhengzebiaodashi]# source paixu.sh Please enter your array, separated by spaces: 8 1 5 9 7 Your array is: 8 1 5 9 7 The array in ascending order is: 1 5 7 8 9 The array in descending order is: 9 8 7 5 1 This is the end of this article about the detailed explanation of Linux commands sort, uniq, and tr tools. For more related Linux commands sort, uniq, and tr tools, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: JS realizes video barrage effect
>>: Detailed explanation of the usage and differences of MySQL views and indexes
This article shares the specific code of videojs+...
Original address: http://www.webdesignfromscratch...
Table of contents Common array methods pop() unsh...
MySQL is a relational database management system....
1. Search for redis image docker search redis 2. ...
1. Convert the json object into a json string, an...
1. Purpose Write a Flask application locally, pac...
Sometimes you need to use links, but you don't...
Table of contents 01 Common Faults 1 02 Common Fa...
1. Background 1.1 Problems A recent product testi...
Table of contents Overview What are callbacks or ...
Copy code The code is as follows: wmode parameter...
This article shares with you how to use Navicat t...
I was playing with CentOS in a VMware virtual mac...
1. Command Introduction The watch command execute...