Shell programming tools (sort, uniq, tr)

sort tool

Sort according to different data types

  • Character sort (default)
  • Number sort

Grammar format

sort [options] parameter

Common options

  • -f: Ignore case
  • -b: Ignore the spaces in front of each line
  • -M: Sort by month
  • -n: sort by number
  • -r: reverse sort
  • -u: equivalent to uniq, which means that only one line of the same data is displayed
  • -t: Specify the separator, use [Tab] to separate by default
  • -o <output file>: dump the sorted results to the specified file
  • -k: Specify the sort area

usage

Sort the accounts in the /etc/passwd file (default character sorting)

[root@server2 ~]# sort /etc/passwd

Insert picture description here

Sort the third column in the /etc/passwd file forward and backward

[root@server2 ~]# sort -n -t ":" -k 3 /etc/passwd

Insert picture description here

[root@server2 ~]# sort -n -t ":" -rk 3 /etc/passwd

Insert picture description here

Export the reverse sort results to the specified file zhfx.txt

[root@server2 ~]# sort -n -t ":" -rk 3 /etc/passwd -o zhfx.txt
[root@server2 ~]# cat zhfx.txt 

Insert picture description here

uniq tool

Common options

  • -c: count
  • -d; only show duplicate lines
  • -u: only display lines that appear once

usage

View automatic merge of duplicate rows

[root@server2 ~]# vi testfilel
[root@server2 ~]# uniq testfilel 

Insert picture description here
Insert picture description here

Repeat count of duplicate rows

[root@server2 ~]# uniq -c testfilel

Only show duplicate rows that appear and show the number of occurrences

[root@server2 ~]# uniq -cd testfilel 

Insert picture description here

Show only lines that occur once

[root@server2 ~]# uniq -u testfilel 

Insert picture description here

tr tool

The specific command statement format of tr is

  • tr [options] [parameters]

Its common options include the following

  • -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 the continuously repeated characters as a single character
  • -t: first delete the characters in the first character set more than the second character set, and replace them

usage

Convert input characters from uppercase to lowercase

[root@server2 ~]# echo "GHHH" | tr 'A-Z' 'a-z'

Insert picture description here

Replace all characters that are not in the first character set

[root@server2 ~]# echo "bcd123bcd123" | tr -c bc 44

Insert picture description here

Delete all characters that are not in the first character set

[root@server2 ~]# echo "happy new year" | tr -d 'ae'  

Insert picture description here

Compress repeated characters in input

[root@server2 ~]# echo "Itttttt was an appleeeeeeeee" | tr -s 'te' 

Insert picture description here

Delete the characters in the first character set that are more than the second character set and replace them

[root@server2 ~]# echo "bcd123bcd123" | tr -t 'bcd' 'e' 
[root@server2 ~]# echo "bcd123bcd123" | tr -t 'bcd' 'eee' 
[root@server2 ~]# echo "bcd123bcd123" | tr -t 'bc' 'efg' 

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49343462/article/details/109669355