Shell入门(六)之Shell pipe(管道)

Shell pipe(管道)命令

pipe(管道)命令使用|界定符号。

pipe管道命令|,仅能处理经由前面一个指令传来的信息,也就是标准输出(standard output)的信息,对于标准错误输出(standard error output)没有处理能力。整体的pipe管道命令可以使用下图表示:

eg:在当前用户执行ls -al | more,可以看到是以more形式显示,如下图:

除此之外,pipe还有一些其他的命令。

撷取命令:cut、grep

  • cut

    选项与参数

    • -d:后面接分隔字符。与-f一起使用。
    • -f:依据-d的分隔字符将一段信息分隔数段,用-f取出第几段的意思。
    • -c:以字符的单位取出固定字符区间

eg:

[zhang@localhost ~]$ echo "hello world" | cut -d "o" -f 1
hell
[zhang@localhost ~]$ echo "hello world" | cut -d "o" -f 2w
[zhang@localhost ~]$ echo "hello world" | cut -d "o" -f 3
rld[zhang@localhost ~]$ echo "hello world" | cut -c 3
l
[zhang@localhost ~]$ echo "hello world" | cut -c 5
o
[zhang@localhost ~]$ echo "hello world" | cut -c 9
r
  • grep

    grep选项与参数:

    • -a:将binary档案以text档案的方式搜寻数据
    • -c:计算找到’搜寻字符串’的次数
    • -i:忽略大小写
    • -n:顺便输出行号
    • -v:反向选择,亦即显示出没有’搜寻字符串’内容的那一行
    • --color=auto:可以将找到的关键字部分加上颜色显示。color有三种参数(auto,always,never)

eg:

现在1.txt输入一些数据,如:

[zhang@localhost ~]$ cat 1.txt
hello world
abcdefg hijklmn
opqrst uvwxyz
abc
hello
world
HEllo1
hello2
world3
[zhang@localhost ~]$ cat 1.txt | grep hello
hello world
hello
hello2

搜索到的hello字段,默认红色,如果加上--color=never可以去掉颜色。

添加-n参数,可以输出行号。

[zhang@localhost ~]$ cat 1.txt | grep hello -n
1:hello world
5:hello
21:hello2

添加-i参数,忽略大小写。

[zhang@localhost ~]$ cat 1.txt | grep hello -i
hello world
hello
HEllo1
hello2

|连续使用

[zhang@localhost ~]$ cat 1.txt | grep o | grep w
hello world
opqrst uvwxyz
world
world3

排序命令:sort、wc、uniq

  • sort

    选项与参数

    • -f:忽略大小写的差异
    • -b:忽略最前面的空格部分
    • -M:以月份的名字来排序
    • -n:使用纯数字进行排序
    • -r:反向排序
    • -u:就是uniq,相同的数据中,仅出现一行代表
    • -t:分隔符,预设是用tab键来分隔
    • -k:以那个区间filed来进行排序

eg:

sort进行排序:

[zhang@localhost ~]$ cat 1.txt | sort
abc
abcdefg hijklmn
hello
HEllo1
hello2
hello world
opqrst uvwxyz
world
world3

-r参数进行反向排序

[zhang@localhost ~]$ cat 1.txt | sort -r
world3
world
opqrst uvwxyz
hello world
hello2
HEllo1
hello
abcdefg hijklmn
abc
  • wc

    如果我们想知道1.txt中有多少行,多少个单词,多少个字符。我们可以使用wc命令。

    选项与参数

    • -l:今列出行
    • -w:今列出多少字(英文单词)
    • -m:多少字符

eg:

[zhang@localhost ~]$ wc 1.txt9 12 79 1.txt
[zhang@localhost ~]$ wc 1.txt -l
9 1.txt
[zhang@localhost ~]$ wc 1.txt -w
12 1.txt
[zhang@localhost ~]$ wc 1.txt -m
79 1.txt
  • uniq

    选项与参数

    • -i:忽略大小写
    • -c:进行计数
[zhang@localhost ~]$ cat 2.txt
hello
Hello
WOrld
abc
abc
ABC
hello1

2.txt进行sort后,进行uniq。

[zhang@localhost ~]$ cat 2.txt | sort | uniq
abc
ABC
hello
Hello
hello1
WOrld

进行sort,使用uniq忽略大小写

[zhang@localhost ~]$ cat 2.txt | sort | uniq -i
abc
hello
hello1
WOrld

进行sort,使用uniq进行计数

[zhang@localhost ~]$ cat 2.txt | sort | uniq -c2 abc1 ABC1 hello1 Hello1 hello11 WOrld

Shell入门(六)之Shell pipe(管道)相关推荐

  1. Shell入门教程:Shell函数详解

    Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以速度很快,另外,Shell还能对函数进行预处理,所以函数的启动比脚本更快. 1.函数定义 ...

  2. Linux Shell脚本入门教程系列之(十六) Shell输入输出重定向

    本文是Linux Shell系列教程的第(十六)篇,更多Linux Shell教程请看:Linux Shell系列教程 Shell中的输出和输入的重定向是在使用中经常用到的一个功能,非常实用,今天就为 ...

  3. Linux Shell脚本入门教程系列之(六)Shell数组

    本文是Linux Shell脚本系列教程的第(六)篇,更多shell教程请看:Linux Shell脚本系列教程 Shell在编程方面非常强大,其数组功能也非常的完善,继上一篇之后,今天就为大家介绍下 ...

  4. 【linux教程(一)】Linux shell入门

    1. linux shell简介 2. bash shell基础命令 3. linux shell的一些基本知识 4. shell变量和传递参数 5. linux中的文件权限 6. linux中的ro ...

  5. 【shell笔记】Linux Shell脚本编程入门知识点全面涵盖

    本文是我对白树明老师shell课程笔记的总结,课程链接:https://www.bilibili.com/video/BV1j541157Sr?from=search&seid=9757674 ...

  6. shell入门学习笔记-15-命令详解: 三剑客之一awk-分支语句、数组

    系列目录与参考文献传送门: shell入门学习笔记-序章 awk分支控制 if admindeMacBook-Pro:~ admin$ seq 5 |awk '{if($0%2) print $0}' ...

  7. php shell脚本怎么写,Shell脚本编程入门

    一.什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_tut for ((i=0; i<10; i++)); do tou ...

  8. 【快速入门并掌握shell脚本编程】

    shell脚本一学就会: 提示:通过此博文可快速掌握shell的基本用法 : 用心学习,一天即可掌握shell 一.SHELL基础: 什么是shell shell是用户与linux内核之间的解释器 [ ...

  9. 【b站黑马程序员学习笔记-shell入门编程】

    使用root用户创建并执行test2.sh,实现创建一个shelltest用户,并在其家目录中新建文件try.html 一. [root@localhost ~]# touch test2.sh [r ...

最新文章

  1. Android画图学习总结(四)——Animation(上)
  2. 专访阿里资深研发工程师窦贤明:PG与商业数据库差距并不明显
  3. 锁定弹出层(jquery语法)
  4. Extjs 入门(03) 折叠||收锉
  5. 基于边缘计算的实时绩效_基于绩效的营销中的三大错误
  6. PowerDesigner生成数据库刷库脚本
  7. 卷积神经网络在tenserflow的实现
  8. Ubuntu 16.04.4 LTS下安装JDK
  9. 剪映专业版 for Mac(全能好用的视频编辑工具)v1.0.11中文版
  10. 科研不是比赛,而是一种对未知和完美的自我追求——跟邢波(Eric Xing)面对面聊科研...
  11. ISO 22000:2018食品安全管理体系介绍、认证及其标准
  12. 书籍翻译 - Fundamentals of Computer Graphics, Fourth Edition 虎书第四版中文翻译
  13. glm/glm.hpp_从GLM到GBM(第2部分)
  14. Navigator的教程
  15. unity 通过摄像机模拟实现小地图
  16. html 图片加载 占位,css实现图片未加载完成时占位显示
  17. guzzle发起请求设置cookie失效
  18. k8s ingress and egress
  19. 关于emacs字体放大问题
  20. mysql 大批量数据查询_mysql 处理 多条件 大批量数据 查询

热门文章

  1. 视频互动直播软件开发中的连麦问题分析 1
  2. C#练习——窗体实现简单计算器,完成加,减,乘,除,取余,简单运算
  3. 【论文阅读】CoRRN: Cooperative Reflection Removal Network
  4. 手把手教你安装vue脚手架
  5. 安装Vue的脚手架时遇到的无法安装问题
  6. TensorFlow - 保存和恢复
  7. 龙之信条一直显示连接服务器中,游戏新消息:龙之信条黑暗觉者官方直播20分钟实机演示...
  8. 【童晶老师《Python游戏趣味编程》在PyCharm中编辑】
  9. 拉格朗日乘子法的由来
  10. CSS3新特性总结及CSS组件、特效汇总