Command1 | Command2 | Command3
管道作用:“|”后面接的一定是命令,命令处理前面传来的正确信息,错误信息忽略,过滤后向后流(类似管道流水)
管道配合常用命令
选取命令:cut,grep
cut:取出一行中我们想要的信息
#-d 以什么分割 #-f 取第几列 xiaoyaz@localhost:~$ cat /etc/passwd |head -n 3 #取前三条 #root:x:0:0:root:/root:/bin/bash #daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin #bin:x:2:2:bin:/bin:/usr/sbin/nologin xiaoyaz@localhost:~$ cat /etc/passwd | cut -d ':' -f1 |head -n 3 #-d 以什么分割,-f 取第几段 #root #daemon #bin
grep:分析这一行是否是我们想要的
#-a binary文件以text文件查询 #-c 找到字符串的次数 #-i 忽略大小写 #-v 反选 #-n 显示行号 xiaoyaz@localhost:~/文档/shell$ cat /etc/passwd |grep 'home' #过滤行有'home'的挑出来 #xiaoyaz:x:1000:1000:xiaoyaz,,,:/home/xiaoyaz:/bin/bash #10haha:x:1002:1002::/home/10haha: #heihei1:x:1003:1003::/home/heihei1: #heihei2:x:1004:1004::/home/heihei2: #heihei3:x:1005:1005::/home/heihei3: #heihei4:x:1006:1006::/home/heihei4: #heihei5:x:1007:1007::/home/heihei5: #ha1:x:1008:1008::/home/ha1: #ha2:x:1009:1009::/home/ha2: #test1:x:1010:1010::/home/test1: #test2:x:1011:1011::/home/test2: #test3:x:1012:1012::/home/test3: #user:x:1013:1013::/home/user: #testt1:x:1014:1014::/home/testt1: #testt2:x:1015:1015::/home/testt2: xiaoyaz@localhost:~/文档/shell$ cat /etc/passwd |grep 'home'|cut -d ':' -f1 #过滤行有'home'的挑出来,以’:‘分割,找到第一列 #xiaoyaz #10haha #heihei1 #heihei2 #heihei3 #heihei4 #heihei5 #ha1 #ha2 #test1 #test2 #test3 #user #testt1 #testt2
排序命令:sort,wc,uniq
sort排序,默认字符
-f忽略大小写 -b前面忽略空格 -M以月份排序 -n数字排序 -r反向排序 -u相同的只出现一行 -t 分隔符 -k 那个区间
uniq删除重复
-i忽略大小写 -c计数 xiaoyaz@localhost:~/文档/shell$ last |cut -d ' ' -f1|sort|uniq -c #1 #5 hadoop #31 reboot #1 wtmp #34 xiaoyaz
wc计数
-l 列出行数
-w 多少个字
-m 多少个字符
xiaoyaz@localhost:~/文档/shell$ last |grep [a-zA-Z]|grep -v 'wtmp'|wc -l #70
双向重定向:tee
#-a 追加 保存ll到txt,并屏幕输出 xiaoyaz@localhost:~/文档/shell$ ll | tee -a tmp.txt
字符替换命令:tr,col,join,paste,expand
tr
-d 删除字符串 -s 替换掉重复的 xiaoyaz@localhost:~/文档/shell$ last |tr -d "-" #删掉'-' #reboot system boot 4.9.07amd64 Sun Mar 3 09:56 00:45 (3+14:49) xiaoyaz@localhost:~/文档/shell$ last |tr [a-z] [A-Z] #大小写替换 #REBOOT SYSTEM BOOT 4.9.0-7-AMD64 SUN MAR 3 09:56 - 00:45 (3+14:49) xiaoyaz@localhost:~/文档/shell$ last |tr -s [a-z] [A-Z] #大小写替换,删除重复字符 #REBOT SYSTEM BOT 4.9.0-7-AMD64 SUN MAR 3 09:56 - 00:45 (3+14:49)
join:将两个文件中有相同数据的贴在一起
-t 默认空格分割,可以指定分割字符 -i 忽略大小写 -1 第一个文件的哪个字段 -2 第二个文件的哪个字段 root@localhost:/home/xiaoyaz# join -t ":" -1 3 /etc/group -2 4 /etc/passwd #0:root:x::root:x:0:root:/root:/bin/bash
paste:两个文件的数据直接贴在一起
-d 以什么分割两个文件,默认tab
- 接收stdin
root@localhost:/home/xiaoyaz# paste /etc/passwd /etc/shadow #testt2:x:1015:1015::/home/testt2: testt2:!:17983:0:99999:7:::
分割命令:split
-b 切割数据的大小,可加单位 -l 按行切割 xiaoyaz@localhost:~/文档/shell$ last |split -l 5 - spl_test_ xiaoyaz@localhost:~/文档/shell$ ll spl_test_* #-rw-r--r-- 1 xiaoyaz xiaoyaz 366 3月 31 22:12 spl_test_aa #-rw-r--r-- 1 xiaoyaz xiaoyaz 366 3月 31 22:12 spl_test_ab #-rw-r--r-- 1 xiaoyaz xiaoyaz 367 3月 31 22:12 spl_test_ac #-rw-r--r-- 1 xiaoyaz xiaoyaz 365 3月 31 22:12 spl_test_ad #-rw-r--r-- 1 xiaoyaz xiaoyaz 365 3月 31 22:12 spl_test_ae #-rw-r--r-- 1 xiaoyaz xiaoyaz 367 3月 31 22:12 spl_test_af #-rw-r--r-- 1 xiaoyaz xiaoyaz 365 3月 31 22:12 spl_test_ag #-rw-r--r-- 1 xiaoyaz xiaoyaz 365 3月 31 22:12 spl_test_ah #-rw-r--r-- 1 xiaoyaz xiaoyaz 365 3月 31 22:12 spl_test_ai #-rw-r--r-- 1 xiaoyaz xiaoyaz 365 3月 31 22:12 spl_test_aj #-rw-r--r-- 1 xiaoyaz xiaoyaz 367 3月 31 22:12 spl_test_ak #-rw-r--r-- 1 xiaoyaz xiaoyaz 367 3月 31 22:12 spl_test_al #-rw-r--r-- 1 xiaoyaz xiaoyaz 365 3月 31 22:12 spl_test_am #-rw-r--r-- 1 xiaoyaz xiaoyaz 369 3月 31 22:12 spl_test_an #-rw-r--r-- 1 xiaoyaz xiaoyaz 38 3月 31 22:12 spl_test_ao
参数带换:xargs
https://www.cnblogs.com/wangqiguo/p/6464234.html
减号'-':stdin或者stdout 某些文件名可用-代替