cat-grep-sed应用案例
案例:
已知文件test.txt內(nèi)容為:
test
liming
xiaoming
請(qǐng)打印出test.txt內(nèi)容時(shí),不包含xiaoming字符串的命令。
創(chuàng)建文件test.txt
[root@hello110 testdata]# cat >>test.txt<<EOF
> test
> liming
> xiaoming
> EOF
實(shí)現(xiàn)
方法一:用head命令
[root@hello110 testdata]# head -2 test.txt?
test
liming
head命令:取頭部的前n行。如果不接參數(shù),默認(rèn)是前10行。
命令:head -2 test.txt 或 head -n 2 test.txt
方法二:grep
grep命令:過(guò)濾器,把想要的或者不想要的分離開(kāi)
想要什么:grep "要的內(nèi)容"
不想要什么:grep -v "不想要的內(nèi)容"
[root@hello110 testdata]# grep "liming" test.txt?
liming
[root@hello110 testdata]# grep -v "liming" test.txt?
test
xiaoming
grep畫(huà)蛇添足的用法:
[root@hello110 testdata]# cat test.txt |grep -v "liming" test.txt?
test
xiaoming
不專(zhuān)業(yè)!
方法三:sed
sed:過(guò)濾。格式:sed [-n] ?'/過(guò)濾的內(nèi)容/處理的命令' ?文件
-n:取消sed的默認(rèn)輸出
-i:改變文件內(nèi)容
處理命令:有很多。p ?print。d ?delete,不刪除內(nèi)容。
[root@hello110 testdata]# sed '/liming/d' test.txt ?
test
xiaoming
[root@hello110 testdata]# cat test.txt ? ? ? ? ? ??
test
liming
xiaoming
[root@hello110 testdata]# sed -n '/liming/p' test.txt?
liming
[root@hello110 testdata]# sed '/liming/p' test.txt ? ?
test
liming
liming
xiaoming
總結(jié)
以上是生活随笔為你收集整理的cat-grep-sed应用案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 运维老鸟教你安装centos6.5如何选
- 下一篇: Oracle修改表结构字段名和字段长度