zoukankan      html  css  js  c++  java
  • 常用管线命令

    博主本人平和谦逊,热爱学习,读者阅读过程中发现错误的地方,请帮忙指出,感激不尽


    主要命令:cut, grep, sort, wc, uniq, tee, tr, col, join, paste, expand, split, xargs


    一、cut

    • 属性:撷取命令
    • 说明:这个指令可以将一段讯息的某一段给他“切”出来~ 处理的讯息是以“行”为单位
    选项与参数: 
    -d :后面接分隔字符。与 -f 一起使用; 
    -f :依据 -d 的分隔字符将一段讯息分区成为数段,用 -f 取出第几段的意思; 
    -c :以字符 (characters) 的单位取出固定字符区间; 
    --complement:补足被选择的字节、字符或字段;
    • 实例1

    将PATH变量取出第五个路径

    [root@test ~]# echo ${PATH}
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    [root@test ~]# echo ${PATH} | cut -d ':' -f 5
    /root/bin
    [root@test ~]# 
    • 实例2

    按指定范围输出

    [root@test ~]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jan 15 14:11 123456789
    
    取得第五个字符以后的所有字符
    [root@test
    ~]# ls | cut -c 5- 56789

    取得第五到八个字符 [root@test ~]# ls | cut -c 5-8 5678 [root@test ~]#
    • 实例3
    用指定分隔符截取指定位置字符
     
    [root@test ~]# echo "1 2 3 4 5">test01.txt
    [root@test ~]# cat test01.txt    
    1 2 3 4 5
    [root@test ~]# cut -d ' ' -f 1 test01.txt   
    1
    [root@test ~]# cut -d ' ' -f2,4 test01.txt   
    2 4
    [root@test ~]# cut -d ' ' -f1,4 test01.txt  
    1 4
    [root@test ~]# cut -d ' ' -f1-4 test01.txt   
    1 2 3 4
    [root@test ~]# cut -d ' ' -f2-4 test01.txt   
    2 3 4
    [root@test ~]#
    • 实例4

    截取指定字段之外的列,也就是取反

    [root@test ~]# cat test01.txt 
    1 2 3 4 5
    [root@test ~]# cut -d ' ' -f2-4 test01.txt  --complement
    1 5
    [root@test ~]# 

     [root@test ~]# cut -d ' ' -f2,4 test01.txt --complement
     1 3 5
     [root@test ~]#

    二、grep

    • 属性:撷取命令
    • 说明:grep 是分析一行讯息, 若当中有我们所需要的信息,就将该行拿出来
    选项与参数: 
    -a :将 binary 文件以 text 文件的方式搜寻数据 
    -c :计算找到 '搜寻字串' 的次数 
    -i :忽略大小写的不同,所以大小写视为相同 
    -n :顺便输出行号 
    -v :反向选择,亦即显示出没有 '搜寻字串' 内容的那一行! 
    --color=auto :可以将找到的关键字部分加上颜色的显示喔! 
    • 实例1

    查看root的行信息以及行数

    [root@test ~]# grep 'root' -n /etc/passwd      
    1:root:x:0:0:root:/root:/bin/bash
    10:operator:x:11:0:operator:/root:/sbin/nologin
    [root@test ~]# 

    三、sort

    属性:排序命令

    说明:sort可以帮助我们进行排序

    -f :忽略大小写的差异,例如 A 与 a 视为编码相同; 
    -b :忽略最前面的空白字符部分; 
    -M :以月份的名字来排序,例如 JAN, DEC 等等的排序方法; 
    -n :使用“纯数字”进行排序(默认是以文字体态来排序的); 
    -r :反向排序; 
    -u :就是 uniq ,相同的数据中,仅出现一行代表; 
    -t :分隔符号,默认是用 [tab] 键来分隔; 
    -k :以那个区间 (field) 来进行排序的意思 
    • 实例1

    账户排序(默认使用首字符排序,首字符相同,以第二位字符进行排序)

    [root@test ~]# cat /etc/passwd  |sort 
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    bin:x:1:1:bin:/bin:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nolog
    ....
    • 实例2
    /etc/passwd 内容是以 : 来分隔的,以第三栏来排序
    [root@test ~]# cat /etc/passwd  |sort  -t ';' -k 3
    adm:x:3:4:adm:/var/adm:/sbin/nologin
    bin:x:1:1:bin:/bin:/sbin/nologin
    chrony:x:998:996::/var/lib/chrony:/sbin/nologin
    daemon:x:2:2:daemon:/sbin:/sbin/nologin
    dbus:x:81:81:System message bus:/:/sbin/nologin
    ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

    四、wc

    • 属性:排序命令
    • 说明:统计
    选项与参数:
     -l :仅列出行; 
     -w :仅列出多少字(英文单字);
     -m :多少字符;
    • 实例1
    [root@test ~]# echo "1 2 3 4 5">test01.txt
    [root@test ~]# cat test01.txt |wc -l      
    1
    [root@test ~]# cat test01.txt |wc -m      
    10
    [root@test ~]# cat test01.txt |wc -w 
    5
    • 实例2

    统计字符串长度

    [root@test ~]# cat test01.txt 
    12345
    [root@test ~]# cat test01.txt |wc -L
    5
    [root@test ~]# 

    五、uniq 

    • 属性:排序命令
    • 说明:去重复
    • 实例1

    去重复(uniq)

    选项与参数:
    -i :忽略大小写字符的不同; 
    -c :进行计数
    [root@test ~]# cat test01.txt 
    1
    1
    2
    2
    3
    3
    4
    5
    10
    [root@test ~]# cat test01.txt |sort -nr|uniq  -c
          1 10
          1 5
          1 4
          2 3
          2 2
          2 1
    [root@test ~]#

    六、tee

    • 属性:双向重导向
    • 说明:去重复
    选项与参数:
     -a :以累加 (append) 的方式,将数据加入 file 当中!
    [root@test ~]# last | tee last.list
    root     pts/0        192.168.247.1    Wed Jan 15 14:40   still logged in   
    root     pts/0        192.168.247.1    Wed Jan 15 14:17 - 14:40  (00:23)    
    reboot   system boot  3.10.0-957.el7.x Wed Jan 15 14:16 - 15:23  (01:06)    
    
    wtmp begins Wed Jan 15 14:16:25 2020
    [root@test ~]# cat last.list 
    root     pts/0        192.168.247.1    Wed Jan 15 14:40   still logged in   
    root     pts/0        192.168.247.1    Wed Jan 15 14:17 - 14:40  (00:23)    
    reboot   system boot  3.10.0-957.el7.x Wed Jan 15 14:16 - 15:23  (01:06)   

    七、tr

    • 属性:字符转换命令
    • 说明:删除与替换
    选项与参数: 
    -d :删除讯息当中的 SET1 这个字串; 
    -s :取代掉重复的字符! 
    • 实例1

    删除指定字符(不会删除源文件)

    [root@test ~]# cat test01.txt
    1 2 3 4 5
    [root@test ~]# cat test01.txt |tr -d 5
    1 2 3 4 
    [root@test ~]#

     [root@test ~]# cat test01.txt |tr -d '[2-4]'
     1   5
     [root@test ~]#

    • 实例2

    大小写替换

    [root@test ~]# echo "a b c D E f">test02.txt
    [root@test ~]# cat test02.txt 
    a b c D E f
    [root@test ~]# cat test02.txt |tr 'a-z' 'A-Z'
    A B C D E F
    [root@test ~]# cat test02.txt |tr 'A-Z' 'a-z'  
    a b c d e f
    [root@test ~]# 
    • 实例3

    字符串去重复

    [root@test ~]# echo "aa a bbb c D d E">test01.txt
    [root@test ~]# cat test01.txt 
    aa a bbb c D d E
    [root@test ~]# cat test01.txt | tr -s '[a-zA-Z]' 
    a a b c D d E
    [root@test ~]# 

    实例4

    权限替换为数字

    [root@test ~]# ls test01.txt  
    test01.txt
    [root@test ~]# ll test01.txt|cut -c 1-10 |tr r 4|tr w 2|tr - 0|awk '{print $1}'
    0420400400
    [root@test ~]# ll test01.txt|cut -c 1-10 |tr r 4|tr w 2|tr - 0|tee t2.txt
    0420400400
    [root@test ~]# 

    八、col

     (略)

    九、join

    属性:文件合并

    说明:加入/参加

    选项与参数: 
    -t :join 默认以空白字符分隔数据,并且比对“第一个字段”的数据,如果两个文件相同,则将两笔数据联成一行,且第一个字段放在第一个! 
    -i :忽略大小写的差异; 
    -1 :这个是数字的 1 ,代表“第一个文件要用那个字段来分析”的意思; 
    -2 :代表“第二个文件要用那个字段来分析”的意思。 

    (略)

    十、paste

    • 属性:内容对比
    • 说明:对比显示文件内容
    选项与参数: 
    -d :后面可以接分隔字符。默认是以 [tab] 来分隔的! 
    - :如果 file 部分写成 - ,表示来自 standard input 的数据的意思。
    • 实例1
    [root@test ~]# paste 1.txt 2.txt 
    1 2 3 4 5       1 2 3 4 5
    a b c d e       a b c d e f g
    [root@test ~]#

    十一、expand

     (略)

    十二、split

    说明:依据文件大小或行数来分区,就可以将大文件分区成为小文件

    选项与参数: 
    -b :后面可接欲分区成的文件大小,可加单位,例如 b, k, m 等; 
    -l :以行数来进行分区。 
    PREFIX :代表前置字符的意思,可作为分区文件的前导文字。 
    • 实例1

    将100M的文件分成50M的文件

    [root@test ~]# dd if=/dev/zero of=testfile bs=1M count=100
    100+0 records in
    100+0 records out
    104857600 bytes (105 MB) copied, 0.879992 s, 119 MB/s
    [root@test ~]# ll -lh
    total 100M
    -rw-r--r-- 1 root root 100M Jan 15 16:58 testfile
    [root@test ~]# split -b 50M testfile 
    [root@test ~]# ls
    testfile  xaa  xab
    [root@test ~]# ll -lh
    total 200M
    -rw-r--r-- 1 root root 100M Jan 15 16:58 testfile
    -rw-r--r-- 1 root root  50M Jan 15 16:59 xaa
    -rw-r--r-- 1 root root  50M Jan 15 16:59 xab
    [root@test ~]# 
    • 实例2

    将划分好的两份小文件合并成原始文件,命名为testfile02

    [root@test ~]# cat xa* >testfile02
    [root@test ~]# ll -lh
    total 300M
    -rw-r--r-- 1 root root 100M Jan 15 16:58 testfile
    -rw-r--r-- 1 root root 100M Jan 15 17:01 testfile02
    -rw-r--r-- 1 root root  50M Jan 15 16:59 xaa
    -rw-r--r-- 1 root root  50M Jan 15 16:59 xab
    [root@test ~]# 
    • 实例3
    使用 ls -al / 输出的信息中,每十行记录成一个文件 
    [root@test local]# pwd
    /usr/local
    [root@test local]# ls -al
    total 0
    drwxr-xr-x. 12 root root 131 Nov 26 21:41 .
    drwxr-xr-x. 13 root root 155 Nov 26 21:41 ..
    drwxr-xr-x.  2 root root   6 Apr 11  2018 bin
    drwxr-xr-x.  2 root root   6 Apr 11  2018 etc
    drwxr-xr-x.  2 root root   6 Apr 11  2018 games
    drwxr-xr-x.  2 root root   6 Apr 11  2018 include
    drwxr-xr-x.  2 root root   6 Apr 11  2018 lib
    drwxr-xr-x.  2 root root   6 Apr 11  2018 lib64
    drwxr-xr-x.  2 root root   6 Apr 11  2018 libexec
    drwxr-xr-x.  2 root root   6 Apr 11  2018 sbin
    drwxr-xr-x.  5 root root  49 Nov 26 21:41 share
    drwxr-xr-x.  2 root root   6 Apr 11  2018 src
    [root@test local]# ls -al | split -l 10 - lsroot        
    [root@test local]# ls
    bin  etc  games  include  lib  lib64  libexec  lsrootaa  lsrootab  sbin  share  src
    [root@test local]# wc -l lsroot*
     10 lsrootaa
      3 lsrootab
     13 total
    [root@test local]# 
    [root@test local]# cat lsrootaa -n
         1  total 0
         2  drwxr-xr-x. 12 root root 131 Nov 26 21:41 .
         3  drwxr-xr-x. 13 root root 155 Nov 26 21:41 ..
         4  drwxr-xr-x.  2 root root   6 Apr 11  2018 bin
         5  drwxr-xr-x.  2 root root   6 Apr 11  2018 etc
         6  drwxr-xr-x.  2 root root   6 Apr 11  2018 games
         7  drwxr-xr-x.  2 root root   6 Apr 11  2018 include
         8  drwxr-xr-x.  2 root root   6 Apr 11  2018 lib
         9  drwxr-xr-x.  2 root root   6 Apr 11  2018 lib64
        10  drwxr-xr-x.  2 root root   6 Apr 11  2018 libexec
    [root@test local]# 
    
    

    十三、xargs

    说明:xargs 可以读入 stdin 的数据,并且以空白字符或断行字符作为分辨,将 stdin 的数据分隔成为 arguments ,很多指令其实并不支持管线命令,因此我们可以通过 xargs 来提供该指令引用 standard input 之用、

    选项与参数: 
    -0 :如果输入的 stdin 含有特殊字符,例如 `, \, 空白键等等字符时,这个 -0 参数 可以将他还原成一般字符。这个参数可以用于特殊状态喔! 
    -e :这个是 EOF (end of file) 的意思。后面可以接一个字串,当 xargs 分析到这个字串时, 就会停止继续工作! 
    -p :在执行每个指令的 argument 时,都会询问使用者的意思; 
    -n :后面接次数,每次 command 指令执行时,要使用几个参数的意思。 当 xargs 后面没有接任何的指令时,默认是以 echo 来进行输出
    • 实例1

    同时执行id(root),id(bin),id(daemon)

    [root@test ~]# cut -d ':' -f 1 /etc/passwd|head -n 3
    root
    bin
    daemon
    [root@test ~]# cut -d ':' -f 1 /etc/passwd|head -n 3|xargs  -n 1 id
      # 通过 -n 来处理,一次给予一个参数,因此上述的结果就 OK 正常的显示,否则会报错
    uid=0(root) gid=0(root) groups=0(root)
    uid=1(bin) gid=1(bin) groups=1(bin)
    uid=2(daemon) gid=2(daemon) groups=2(daemon)
    [root@test ~]# 
    • 实例2
    同上,但是每次执行 id 时,都要询问使用者是否动作?
    [root@test ~]# cut -d ':' -f 1 /etc/passwd|head -n 3|xargs  -p -n 1 id 
    id root ?...y
    uid=0(root) gid=0(root) groups=0(root)
    id bin ?...y
    uid=1(bin) gid=1(bin) groups=1(bin)
    id daemon ?...y
    uid=2(daemon) gid=2(daemon) groups=2(daemon)
    [root@test ~]#
    •  实例3

    行转列

    [root@test ~]# ps -ef  | grep pyth|cut -c 5-15
           6632
           8204
    [root@test ~]# ps -ef  | grep pyth|cut -c 5-15|xargs 
    6632 8207
    [root@test ~]#

    转载需注明出处 

    水果大佬
  • 相关阅读:
    ESAPI = Enterprise Security API
    WingIDE中调试GAE(google app engine)
    C# 发送Http请求 WebClient类
    [转]使用Google App Engine Helper for Django
    装上Window7
    最近遇到个关于接口的奇怪的问题
    JNDI概述(转载)
    Google App Engine 中通过自定义Django的filter解决时区问题
    C# string与byte[]互转
    Python天天美味(33) 五分钟理解元类(Metaclasses)[转]
  • 原文地址:https://www.cnblogs.com/tanshouke/p/12196292.html
Copyright © 2011-2022 走看看