『忘了再學』Shell基礎 — 31、字符處理相關命令

1、排序命令sort

(1)sort命令介紹

sort命令可針對文本文件的內容,以行為單位來排序。

命令格式如下:

[root@localhost ~]# sort [選項] 文件名

選項:

  • -f:忽略大小寫。
  • -b:忽略每行前面的空白部分。
  • -n:以數值型進行排序,sort命令默認使用字符串型排序。
  • -r:反向排序。
  • -u:刪除重複行。就是uniq命令。
  • -t:指定分隔符,sort命令默認的分隔符是製表符。
  • -k [n,m]:按照指定的字段範圍排序。從第n字段開始,m字段結束(默認到行尾)。

(2)練習

以下練習默認文本內容如下:

java    haha    3
python  lala    77
shell   dudu    23
hello   world   12
linxu   xixi    6

1)示例1

sort命令默認是用每行開頭第一個字符來進行排序的。

執行sort排序命令:

# 默認按行首字符進行排序
[root@localhost tmp]# sort test.txt
hello   world   12
java    haha    3
linxu   xixi    6
python  lala    77
shell   dudu    23

如果想要反向排序,請使用-r選項:

# 反向排序
[root@localhost tmp]# sort -r test.txt
shell   dudu    23
python  lala    77
linxu   xixi    6
java    haha    3
hello   world   12

2)示例2

按照文檔中,每行的指定字段進行排序。

需要使用-k選項:

# -k 2,2表示:指定按照第二個字段排序
# 2,2表示第2個字段開始,到第2個字段結束
[root@localhost tmp]# sort -k 2,2 test.txt
shell   dudu    23
java    haha    3
python  lala    77
hello   world   12
linxu   xixi    6

注意:文本中字段之間的分隔是製表符,默認識別。

3)示例3

按照數字進行排序,根據文本中的內容,我們需要按照第三列進行排序。

[root@localhost tmp]# sort -k 3,3 test.txt
hello   world   12
shell   dudu    23
java    haha    3
linxu   xixi    6
python  lala    77

我們發現按照第三列內容進行排序的結果,有點不正確,3和6怎麼排在12的後邊了。

是因為sort命令並沒有把第三列的數據當作數字,默認是識別成字符串,所以是按照字符串的規則來排序的,也就是按第一位的數字進行排序的。

我們需要添加sort命令的-n選項,就可以解決上述問題了。也就是讓sort命令把第三列按數值進行排序。

[root@localhost tmp]# sort -n -k 3,3 test.txt
java    haha    3
linxu   xixi    6
hello   world   12
shell   dudu    23
python  lala    77

4)示例4

如果想要指定排序的字段,並且文本中字段間的分隔符不是製表符,這個時候就需要使用-t選項指定分隔符,並使用-k選項指定字段號。

如下面文本:

java:haha:3
python:lala:77
shell:dudu:23
hello:world:12
linxu:xixi:6

需求:按文本內容中的第三列數字進行排序。

[root@localhost tmp]# sort -t ":" -n -k 3,3 test.txt
java:haha:3
linxu:xixi:6
hello:world:12
shell:dudu:23
python:lala:77

2、取消重複行命令uniq

uniq命令是用來取消重複行的命令,其實和sort -u選項是一樣的。

命令格式如下:

[root@localhost ~]# uniq [選項] 文件名

選項:
    -i:忽略大小寫。

練習:

student.txt文本內容如下:

ID      Name    Python  Linux   MySQL   Java
1       Tangs   88      87      86      85.55
2       Sunwk   99      98      97      96.66
2       Sunwk   99      98      97      96.66
3       Zhubj   77      76      75      74.44
3       Zhubj   77      76      75      74.44
4       Shahs   66      65      64      63.33
4       Shahs   66      65      64      63.33

我們可以看到student.txt文本的ID為2、3、4的信息有重複。

使用uniq命令刪除重複的行後,有如下輸出結果:

[root@localhost tmp]# uniq student.txt
ID      Name    Python  Linux   MySQL   Java
1       Tangs   88      87      86      85.55
2       Sunwk   99      98      97      96.66
3       Zhubj   77      76      75      74.44
4       Shahs   66      65      64      63.33

更多參考可以查看://www.runoob.com/linux/linux-comm-uniq.html

3、統計命令wc

wc命令是統計文檔中行數,字符數,位元組數等信息。

命令格式如下:

[root@localhost ~]# wc [選項] 文件名
選項:
    -l:只統計行數
    -w:只統計單詞數
    -m:只統計字符數

練習1:

在默認的情況下,wc命令將計算指定文件的行數、字數,以及位元組數。

# 行數為5、單詞數30、位元組數124
[root@localhost tmp]# wc student.txt
  5  30 124 student.txt

練習2:

查看特定的統計信息,只看行數和位元組數。

# 行數為5、位元組數124
[root@localhost tmp]# wc -lm student.txt
  5 124 student.txt

練習3:

同時查看多個文件的統計信息。

[root@localhost tmp]# wc student.txt test2.txt
  5  30 124 student.txt
  8  37 318 test2.txt
 13  67 442 總用量

更多參看可以查看://www.runoob.com/linux/linux-comm-wc.html