小白入門之十一:linux系統(tǒng)中文件內(nèi)容抽取字段、統(tǒng)計、排序
目的
熟練使用cut、sort、uniq、wc等命令應用。 cut命令功能:從文件的每一行截取一段內(nèi)容; sort命令功能:把文本文件的行排序; uniq命令功能:報告或忽略重復的行; wc命令功能:為文件打印行數(shù)、單詞數(shù)、字節(jié)數(shù)。
前提
可用的centos7系統(tǒng),連接網(wǎng)絡。
命令介紹
1、cut命令:按列抽取文本內(nèi)容
【例1】截取/etc/passwd文件第一行,以冒號為分隔符,抽取第7個字段
[root@Magedu ~]# head -1 /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@Magedu ~]# head -1 /etc/passwd | cut -d: -f7
/bin/bash
2、sort命令:文本排序
【例2】以1.sh文件一行內(nèi)容的空格分隔,按第3段從大到小排序
[root@Magedu ~]# 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
[root@Magedu ~]# 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命令:文本數(shù)據(jù)統(tǒng)計
【例3】統(tǒng)計/etc/pass文件有多少行
[root@Magedu ~]# cat /etc/passwd | wc -l
50
4、uniq命令:文本去重
【例4】統(tǒng)計2.sh文件中相同內(nèi)容的行出現(xiàn)的次數(shù)
[root@Magedu ~]# cat 2.sh
this is 111 line
this is 111 line
this is 111 line
this is 111 line
this is 111 line
[root@Magedu ~]# uniq -c 2.sh
5 this is 111 line
文章來源于網(wǎng)絡,侵刪!