linux之管道符详解

linux之管道符 ’ | ’ 操作详解 管道符主要用于多重命令处理,前面命令的打印结果作为后面命令的输入。简单点说就是,就像工厂的流水线一样,进行完一道工序后,继续传送给下一道工序处理… 举个栗子:对hello.sh文件进行排序去重以后找出包含"better"的行 命令为:cat hello.sh | sort | uniq | grep 'better’ 查看文本 排序 去重 过滤

1 ![image](https://img-hello-world.oss-cn-beijing.aliyuncs.com/imgs/3d57bd1d42f8f585bccf2aecd1f651b9.png)

【1】第一道工序——查看文本 首先使用cat命令查看文本,打印到屏幕上内容即为cat命令的输出结果

1 [root@linuxforliuhj test]# cat hello.sh 2hello this is linux 3be better 4be better 5i am lhj 6hello this is linux 7i am lhj 8i am lhj 9be better 10i am lhj 11have a nice day 12have a nice day 13hello this is linux 14hello this is linux 15have a nice day 16zzzzzzzzzzzzzz 17dddddddd 18gggggggggggggggggggg 19[root@linuxforliuhj test]#

【2】第二道工序——排序 将前面cat命令输出的结果通过管道丢给sort命令,所以sort命令是对前面cat命令输出的文本进行排序

1 [root@linuxforliuhj test]# cat hello.sh | sort 2be better 3be better 4be better 5dddddddd 6gggggggggggggggggggg 7have a nice day 8have a nice day 9have a nice day 10hello this is linux 11hello this is linux 12hello this is linux 13hello this is linux 14i am lhj 15i am lhj 16i am lhj 17i am lhj 18zzzzzzzzzzzzzz 19[root@linuxforliuhj test]#

【3】第三道工序——去重 前面介绍uniq的文章中提到,sort跟uniq结合使用才能有效去重,所以通过管道将sort处理后输出的文本丢给uniq处理,所以uniq处理的是排序好的文本,可以进行有效去重

1 [root@linuxforliuhj test]# cat hello.sh | sort | uniq 2be better 3dddddddd 4gggggggggggggggggggg 5have a nice day 6hello this is linux 7i am lhj 8zzzzzzzzzzzzzz 9[root@linuxforliuhj test]#

【4】第四道工序——过滤 最后一步过滤则同样是将前面命令即uniq命令处理后输出的文本进行过滤

1 [root@linuxforliuhj test]# cat hello.sh | sort | uniq | grep 'better' 2be better 3[root@linuxforliuhj test]#

重点来了! 重点来了! 重点来了! 以上的cat、sort、uniq、grep等命令均支持管道符,是因为这些命令均可从标准输入中读取要处理的文本(即从标准输入中读取参数);而对于部分命令,例如rm、kill等命令则不支持从标准输入中读取参数,只支持从命令行中读取参数(即rm命令后面必须指定删除的文件或者目录,kill命令后面必须要指定杀死的进程号等) 那什么样的命令支持管道,什么样的命令不支持管道呢? 一般情况下,处理文本的命令,例如sort、uniq、grep、awk、sed等命令均支持管道;像rm、ls这类的不是处理文本的命令均不支持管道

1 [root@linuxforliuhj test]# cat hello.sh | sort 2be better 3be better 4be better 5dddddddd 6gggggggggggggggggggg 7have a nice day 8have a nice day 9have a nice day 10hello this is linux 11hello this is linux 12hello this is linux 13hello this is linux 14i am lhj 15i am lhj 16i am lhj 17i am lhj 18zzzzzzzzzzzzzz 19[root@linuxforliuhj test]#

sort后面没有参数时,则对管道符丢给它的前一个命令的输出结果进行处理(即前一个命令的标准输出作为本次命令的标准输入)

1 [root@linuxforliuhj test]# ls 2beifen.txt hello.sh mk read.ln read.sh read.txt sub.sh 3[root@linuxforliuhj test]# ls | grep read.sh 4read.sh 5[root@linuxforliuhj test]# ls | grep read.sh | rm 6rm: missing operand 7Try 'rm --help' for more information. 8[root@linuxforliuhj test]#

当rm后面不指定删除的文件时,则会报错丢失参数,所以,rm等命令不支持从标准输入读取参数,只支持在命令行指定参数,即指定删除的文件。 标准输入和命令行参数那个优先? 有如下两个文件

1 [root@linuxforliuhj test]# cat a.txt 2aaaa 3dddd 4cccc 5bbbb 6[root@linuxforliuhj test]# cat b.txt 71111 83333 94444 102222 11[root@linuxforliuhj test]#

执行命令:cat a.txt | sort

1 [root@linuxforliuhj test]# cat a.txt | sort 2aaaa 3bbbb 4cccc 5dddd 6[root@linuxforliuhj test]#

当sort的命令行参数为空时,默认使用前一个命令的输出结果作为本次命令的输入 执行命令:cat a.txt | sort b.txt

1 [root@linuxforliuhj test]# cat a.txt | sort b.txt 21111 32222 43333 54444 6[root@linuxforliuhj test]#

可以看到,当sort的命令行参数(此处为b.txt)不为空时,sort不会读取标准输入里的参数,而时读取命令行参数 执行命令:cat a.txt | sort b.txt -

1 [root@linuxforliuhj test]# cat a.txt | sort b.txt - 21111 32222 43333 54444 6aaaa 7bbbb 8cccc 9dddd 10[root@linuxforliuhj test]#

" - "表示标准输入,即命令cat a.txt 的输出,相当与对文件b.txt和标准输入一起进行排序,相当于sort a.txt b.txt

1 [root@linuxforliuhj test]# sort a.txt b.txt 21111 32222 43333 54444 6aaaa 7bbbb 8cccc 9dddd 10[root@linuxforliuhj test]#

如果你对linux centos系统感兴趣,想在上面搭建一些服务的服务器的话,可以后台私信我,我白天回复,

点赞
收藏

评论区

加载中...

相关推荐

PHP命令行下的世界

PHP作为一门web开发语言,通常情况下我们都是在WebServer中运行PHP,使用浏览器访问,因此很少关注其命令行操作以及相关参数的使用,但是,特别是在类Unix操作系统上,PHP可以作为一门脚本语言执行与shell类似的处理任务。php命令行(CLI)参数详解查看PHP的所有命令行参数,使用phph命令。我们将会对大部分常用的

Linux下查看CPU型号,内存大小,硬盘空间的命令(详解)

1查看CPU1.1查看CPU个数\cat/proc/cpuinfo|grep"physicalid"|uniq|wcl2\\uniq命令:删除重复行;wc–l命令:统计行数\\1.2查看CPU核数\cat/proc/cpuinfo|grep"cpucores"|uniqc

Linux查看进程和删除进程

基本命令讲解在Linux命令平台输入12个字符后按下Tab键会自动补全后面的部分(前提是要有这个东西,例如在装了tomcat的前提下,输入tomcat的to按table)。ps命令用于查看当前正在运行的进程。grep是搜索例如:psef|grepjava表示查看所有进程里CMD是java的进程信息ps

Linux Shell 文本处理工具集锦

本文将介绍Linux下使用Shell处理文本时最常用的工具:find、grep、xargs、sort、uniq、tr、cut、paste、wc、sed、awk;提供的例子和参数都是最常用和最为实用的;我对shell脚本使用的原则是命令单行书写,尽量不要超过2行;如果有更为复杂的任务需求,还是考虑python吧;find文件查找

Linux企业运维人员最常用150个命令汇总 [转]

近来发现新手学习Linux记不住命令,不会分类、不会筛选重点,胡子眉毛一把抓当然记不住了。特别整理Linux运维最常用150个命令和大家分享,大家学习命令不用在盲目了,根据分类,然后逐步学习!命令功能说明线上查询及帮助命令(2个)man查看命令帮助,命令的词典,更复杂的还有info,但不常用。help查看Linux内置命令的帮

Guava

背景原有的去重方案是:1.使用linux命令去重缺点1.出现问题只能重来,控制粒度很粗。2.程序与操作系统过渡耦合,如果系统中sort或者uniq命令出现问题,则去重功能不能使用。3.使得pushopt的用户数据以文件的形式存在,不方便多主机、操作系统共享