cat-grep-sed应用案例
2016-10-27 16:21
176 查看
案例:
已知文件test.txt内容为:
test
liming
xiaoming
请打印出test.txt内容时,不包含xiaoming字符串的命令。
创建文件test.txt
[root@hello110 testdata]# cat >>test.txt<<EOF
> test
> liming
> xiaoming
> EOF
实现
方法一:用head命令
[root@hello110 testdata]# head -2 test.txt
test
liming
head命令:取头部的前n行。如果不接参数,默认是前10行。
命令:head -2 test.txt 或 head -n 2 test.txt
方法二:grep
grep命令:过滤器,把想要的或者不想要的分离开
想要什么:grep "要的内容"
不想要什么:grep -v "不想要的内容"
[root@hello110 testdata]# grep "liming" test.txt
liming
[root@hello110 testdata]# grep -v "liming" test.txt
test
xiaoming
grep画蛇添足的用法:
[root@hello110 testdata]# cat test.txt |grep -v "liming" test.txt
test
xiaoming
不专业!
方法三:sed
sed:过滤。格式:sed [-n] '/过滤的内容/处理的命令' 文件
-n:取消sed的默认输出
-i:改变文件内容
处理命令:有很多。p print。d delete,不删除内容。
[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
已知文件test.txt内容为:
test
liming
xiaoming
请打印出test.txt内容时,不包含xiaoming字符串的命令。
创建文件test.txt
[root@hello110 testdata]# cat >>test.txt<<EOF
> test
> liming
> xiaoming
> EOF
实现
方法一:用head命令
[root@hello110 testdata]# head -2 test.txt
test
liming
head命令:取头部的前n行。如果不接参数,默认是前10行。
命令:head -2 test.txt 或 head -n 2 test.txt
方法二:grep
grep命令:过滤器,把想要的或者不想要的分离开
想要什么:grep "要的内容"
不想要什么:grep -v "不想要的内容"
[root@hello110 testdata]# grep "liming" test.txt
liming
[root@hello110 testdata]# grep -v "liming" test.txt
test
xiaoming
grep画蛇添足的用法:
[root@hello110 testdata]# cat test.txt |grep -v "liming" test.txt
test
xiaoming
不专业!
方法三:sed
sed:过滤。格式:sed [-n] '/过滤的内容/处理的命令' 文件
-n:取消sed的默认输出
-i:改变文件内容
处理命令:有很多。p print。d delete,不删除内容。
[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
相关文章推荐
- [收藏] 正则表达式的入门与应用,1~4篇!grep/sed/perl/awk
- grep 、sed、awk的一些应用
- Linux下正则表达式以及几种工具:grep,sed,awk,cut,sort,uniq的简单应用
- grep,sed,awk与简单正则表达式应用
- Linux shell命令(2) - cat/cut/paste/sed/tr/grep/sort/uniq
- linux基础篇-07,linux文本处理cat more less head tail sort uniq grep cut jion sed awk
- sed配合正则表达式应用案例
- grep 、sed、awk的一些应用
- awk-grep-sed简单使用总结(正则表达式的应用)
- 正则在sed和grep中的应用
- cat、tail、head、grep、sed查看文件任意几行的数据
- 常用的查看文件行数的命令,cat,wc,sed,awk,grep
- [收藏] 正则表达式的入门与应用,1~4篇!grep/sed/perl/awk
- 正则表达式(grep)元字符及模式匹配的介绍及sed高级应用选项
- sed 应用案例
- 【Linux】cat、tail、head、grep、sed查看文件任意几行的数据
- linux shell 系列 sed 命令基本用法实例-①set命令选项的应用案例
- sed应用案例
- 学习cat sort grep cut sed uniq find less
- [收藏] 正则表达式的入门与应用,1~4篇!grep/sed/perl/awk