1. 程式人生 > >Centos7文字處理工具

Centos7文字處理工具

## 實驗一:檢視檔案內容和比較兩檔案


**目的**


熟練使用cat、less、head、tail、diff等命令。


 


**前提**


可用的centos7系統,連線網路。


 


**命令介紹**


 


**1、cat命令:檢視檔案全部內容**


【例1】檢視1.sh檔案內容


```

[[email protected] ~]# cat 1.sh 


this is 111 line


this is 222 line


this is 333 line


this is 444 line


this is 555 line


this is 666 line


this is 777 line


this is 888 line


this is 999 line

```


 




**2、less命令:分頁顯示檔案內容**


【例2】分頁檢視/var/log/messages檔案,檔案最後不退出


```

[[email protected] ~]# less /var/log/messages

```


退出按q鍵。


 


**3、head命令:檢視檔案首部的內容**


【例3】檢視1.sh檔案的前3行內容


```

[[email protected] ~]# head -3 1.sh 


this is 111 line


this is 222 line


this is 333 line

```


 


**4、tail命令:檢視檔案尾部的內容**


【例4】檢視1.sh檔案的後3行內容


```

[[email protected] ~]# tail -3 1.sh 


this is 777 line


this is 888 line


this is 999 line

```


 


【例5】監視檢視1.sh檔案尾部是否有內容增加


```

[[email protected] ~]# tail -f 1.sh

```


按ctrl+c鍵退出。


 


**5、diff命令:比較兩檔案**


【例6】比較1.sh和2.sh兩檔案的不同


```

[[email protected] ~]# diff 1.sh 2.sh


1,8d0


< this is 111 line


< this is 222 line


< this is 333 line


< this is 444 line


< this is 555 line


< this is 666 line


< this is 777 line


< this is 888 line


9a2,9


> this is 888 line


> this is 777 line


> this is 666 line


> this is 555 line


> this is 444 line


> this is 333 line


> this is 222 line


> this is 111 line

```


------


## **實驗二:指定檔案內容抽取欄位、統計、排序**


 


**目的**


熟練使用cut、sort、uniq、wc等命令應用。


 


**前提**


可用的centos7系統,連線網路。


 


**命令介紹**


 


**1、cut命令:按列抽取文字內容**


【例1】擷取/etc/passwd檔案第一行,以冒號為分隔符,抽取第7個欄位


```

[[email protected] ~]# head -1 /etc/passwd 


root:x:0:0:root:/root:/bin/bash


[[email protected] ~]# head -1 /etc/passwd | cut -d: -f7


/bin/bash

```


 


**2、sort命令:文字排序**


【例2】以1.sh檔案一行內容的空格分隔,按第3段從大到小排序


```

[[email protected] ~]# cat 1.sh 


this is 111 line


this is 222 line


this is 333 line


this is 444 line


this is 555 line


this is 666 line


this is 777 line


this is 888 line


this is 999 line


[[email protected] ~]# cat 1.sh |sort -k3 -r 


this is 999 line


this is 888 line


this is 777 line


this is 666 line


this is 555 line


this is 444 line


this is 333 line


this is 222 line


this is 111 line

```


 


**3、wc命令:文字資料統計**


【例3】統計/etc/pass檔案有多少行


```

[[email protected] ~]# cat /etc/passwd | wc -l


50

```


 


**4、uniq命令:文字去重**


【例4】統計2.sh檔案中相同內容的行出現的次數


```

[[email protected] ~]# cat 2.sh 


this is 111 line


this is 111 line


this is 111 line


this is 111 line


this is 111 line


[[email protected] ~]# uniq -c 2.sh 


      5 this is 111 line

```


------




## 實驗三:grep命令和正則表示式應用


**目的**:


熟練使用grep和正則表示式的應用。


 


**前提**


可用的centos7系統,連線網路。


 


**命令介紹**


 


**1、grep命令:根據指定的匹配模式對文字內容進行搜尋**


【例1】查詢/etc/passwd檔案裡包含root字串的行


```

[[email protected] ~]# grep root /etc/passwd


root:x:0:0:root:/root:/bin/bash


operator:x:11:0:operator:/root:/sbin/nologin

```


 


【例2】查詢2.sh檔案裡顯示不包含111字串的行


```

[[email protected] ~]# cat 2.sh 


this is 111 line


this is 222 line


this is 333 line


this is 444 line


this is 555 line


[[email protected] ~]# grep -v 111 2.sh 


this is 222 line


this is 333 line


this is 444 line


this is 555 line

```


 




【例3】顯示/etc/passwd檔案中以bash結尾的行


```

[[email protected] ~]# grep  'bash$' /etc/passwd


root:x:0:0:root:/root:/bin/bash


lsj:x:1000:1000:lsj:/home/lsj:/bin/bash


linux:x:1004:1004::/home/linux:/bin/bash


liubei:x:1005:1005::/home/liubei:/bin/bash


zhangfei:x:1006:1006::/home/zhangfei:/bin/bash


guanyu:x:1007:1007::/home/guanyu:/bin/bash

```


 


【例4】找出“ldd /usr/bin/cat”命令的結果中的檔案路徑


```

[[email protected] ~]# ldd /usr/bin/cat | grep -o '/[^[:space:]]\+'


/lib64/libc.so.6


/lib64/ld-linux-x86-64.so.2

```


 


【例5】找出ifconfig命令結果中所有IPv4地址


```

[[email protected] ~]# ifconfig ens33|grep -o "\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}"


172.18.118.155


255.255.0.0


172.18.255.255

```


 


【例6】將此字串:welcome to  han linux 中的每個字元去重並排序,重複次數多的排到前面


```

[[email protected] ~]# echo welcome to  han linux|grep -o "."|sort|uniq -c|sort -nr


      3 e


      3  


      2 u


      2 o


      2 m


      2 l


      1 x


      1 w


      1 t


      1 n


      1 i


      1 g


      1 d


      1 c


      1 a

```


 


**2、egrep命令:同grep命令,但支援擴充套件的正則表示式**


【例8】使用egrep取出/etc/rc.d/init.d/functions路徑的目錄名


```

[[email protected] ~]# echo /etc/rc.d/init.d/functions |egrep -o "^[/].*/"


/etc/rc.d/init.d/

```


------