Detailed explanation of Linux commands sort, uniq, tr tools

Detailed explanation of Linux commands sort, uniq, tr tools

Sort Tool

The Linux sort command is used to sort the contents of text files.
Sort can sort the contents of text files in lines.

sort common options

b Ignore the space characters at the beginning of each line.
-c Checks if the files are sorted in order.
-d When sorting, process English letters, numbers, and space characters, and ignore other characters.
-f Treat lowercase letters as uppercase when sorting.
-i When sorting, ignore all characters except ASCII characters between 040 and 176.
-m merges several sorted files.
-M Sort the first three letters by the abbreviation of the month.
-n Sort by numerical value.
-u means unique, and the output is deduplicated.
-o<output file> saves the sorted results to the specified file.
-r Sort in reverse order.
-t<separator character> specifies the field separator character used when sorting.
+<starting field>-<ending field> Sort by the specified field, ranging from the starting field to the field before the ending field.
–help Show help.
–version Display version information

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.
-c or --count displays the number of times the line appears next to each column.
-d or --repeated displays only repeated rows and columns.
-f <field> or --skip-fields=<field> Ignore the specified fields for comparison.
-s<character position> or --skip-chars=<character position> ignores the specified characters in the comparison.
-u or --unique displays only the rows that appear once.
-w<character position> or --check-chars=<character position> specifies the characters to be compared.
–help Show help.
–version Display version information.
[Input file] Specify the sorted text file. If this is not specified, data is read from the standard;
[Output File] Specify the output file. If this option is not specified, the content is displayed to the standard output device (display terminal)

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;
-d: delete all characters belonging to the first character set;
-s: Represent consecutive repeated characters as a single character;
-t: first delete the characters in the first character set that are more than the characters in the second 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:
  • How to use Linux tr command
  • Linux traceroute command usage detailed explanation
  • The difference and usage of Ctrl+z, Ctrl+c and Ctrl+d in Linux commands
  • Trash-Cli: Command-line Recycle Bin Tool on Linux
  • Detailed explanation of strace command for Linux application debugging
  • Detailed explanation of the usage of tree command under Linux
  • Linux shell tr ​​command detailed explanation
  • Detailed explanation of the strings command in Linux
  • One shell command a day Linux text operation series - tree command detailed explanation
  • Use of Linux tr command

<<:  JS realizes video barrage effect

>>:  Detailed explanation of the usage and differences of MySQL views and indexes

Recommend

Web lesson plans, lesson plans for beginners

Teaching Topics Web page Applicable grade Second ...

Some common mistakes with MySQL null

According to null-values, the value of null in My...

Specific use of lazy loading and preloading in js

Delayed loading (lazy loading) and preloading are...

Detailed explanation of how to efficiently import multiple .sql files into MySQL

MySQL has multiple ways to import multiple .sql f...

React Class component life cycle and execution order

1. Two ways to define react components 1. Functio...

Practice of using SuperMap in Vue

Table of contents Preface Related Materials Vue p...

Introduction to Semantic HTML Tags

In the past few years, DIV+CSS was very popular in...

MYSQL's 10 classic optimization cases and scenarios

Table of contents 1. General steps for SQL optimi...

How to set directory whitelist and IP whitelist in nginx

1. Set a directory whitelist: Do not set restrict...

Linux parted disk partition implementation steps analysis

Compared with fdisk, parted is less used and is m...

Detailed process of deploying Docker to WSL2 in IDEA

The local environment is Windows 10 + WSL2 (Ubunt...

Unicode signature BOM detailed description

Unicode Signature BOM - What is the BOM? BOM is th...

Vue application example code based on axios request encapsulation

Table of contents What is axios? Axios request ty...