管道命令與連續(xù)命令不同,連續(xù)命令中的各個命令不存在相關(guān)性只是順序執(zhí)行。
對于管道命令來說 cmd1|cmd2.
cmd2需要 cmd1產(chǎn)生的輸出流作為 cmd2的輸入流,命令之間存在很強的依賴關(guān)系,并且管道命令只能處理正確的輸出數(shù)據(jù)流
從某一行將一段信息切出來
語法:cut –d ‘分割字符’ -f field
cut –c 字符范圍
選項與參數(shù):
-d:后接分割字符與-f 連用
-f:獲取經(jīng)-d 分割后的第幾個字段
-c:以字符的單位取出固定字符區(qū)間,適用于排列正確的信息
選取范圍 a-b 如果是從第 a 個字符到最后可寫成 a-
說明:cut 可以進行單行與多行分割,對于多行每一行都看做單獨的一行分割與獲取 field
舉例1:單行分割
[root@localhost ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
[root@localhost ~]# echo $PATH |cut -d ':' -f 1
/usr/kerberos/sbin
[root@localhost ~]# echo $PATH |cut -d '
/usr/kerberos/sbin:/usr/local/sbin
舉例2:多行分割
[root@localhost ~]# last -5
root pts/1 :0.0 Wed Mar 5 09:41 still logged in
root :0 Wed Mar 5 09:40 still logged in
root :0 Wed Mar 5 09:40 - 09:40 (00:00)
reboot system boot 2.6.18-371.el5 Wed Mar 5 09:20 (05:08)
root pts/1 :0.0 Tue Mar 4 15:27 - crash (17:53)
[root@localhost ~]# last -5|cut -d ' ' -f 1
root
root
root
reboot
root
舉例3:范圍選取
[root@localhost ~]# export
declare -x COLORTERM="gnome-
declare -x DBUS_SESSION_BUS_
declare -x DESKTOP_SESSION="
declare -x DESKTOP_STARTUP_I
declare -x DISPLAY=":0.0"
[root@localhost ~]# export|cut -c 12-
COLORTERM="gnome-terminal"
DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-OeMZpvhP93,guid=30f2d841bcc5b92980611600531680a3"
DESKTOP_SESSION="default"
DESKTOP_STARTUP_ID=""
DISPLAY=":0.0"
分析一行信息,若當中存在我們需要的信息,則將該行輸出,grep 后還可接正則表達式或通配符進行查詢。
語法:grep [-acinv] [-A] [-B] [--color=auto] ‘查找字符串’ filename
選項與參數(shù):
-a:將 binary 文件以 text 文件方式查找數(shù)據(jù)
-c:計算‘查找字符串’次數(shù)
-i:忽略大小寫
-n:輸出行號
-v:反向選擇
-A:后面可跟數(shù)字,代表除了本行外,后續(xù)的 n 行也都列出來
-B: 后面可跟數(shù)字,代表除了本行外,前面的 n 行也都列出來
--color=auto: 關(guān)鍵字部分添加顏色
舉例:
[root@localhost ~]# last -3|grep 'root'
root pts/1 :0.0 Wed Mar 5 09:41 still logged in
root :0 Wed Mar 5 09:40 still logged in
root :0 Wed Mar 5 09:40 - 09:40 (00:00)
[root@localhost ~]# last -5|grep -vn 'root'
4:reboot system boot 2.6.18-371.el5 Wed Mar 5 09:20 (05:33)
6:
7:wtmp begins Fri Feb 14 10:32:51 2014
[root@localhost ~]# last |grep -c 'root'
84
[root@localhost ~]# last -5|grep -n 'roo*' = >通配符查找
1:root pts/1 :0.0 Wed Mar 5 09:41 still logged in
2:root :0 Wed Mar 5 09:40 still logged in
3:root :0 Wed Mar 5 09:40 - 09:40 (00:00)
5:root pts/1 :0.0 Tue Mar 4 15:27 - crash (17:53)
sort 可以按照不同的數(shù)據(jù)類型來排序,例如按數(shù)字或文字排序,排序結(jié)果也受語系編碼的影響,例如有的語系字符是這么排序的 AaBbCc….建議語系使用 LANG=C
語法:sort [-fbMnrtuk]文件或輸入流
選項與參數(shù):
-f:忽略大小寫
-b:忽略最前面的空格
-M:以月份(英文)來排序
-r:反向排序
-t:分隔符與-k 連用
-u:就是 uniq
-k:以那個 field 的進行排序
舉例1:
[root@localhost ~]# cat /etc/passwd |sort -
avahi:x:70:70:Avahi daemon:/:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
cimsrvr:x:100:500:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
舉例2:用‘:’分割第三段進行排序
[root@localhost ~]# cat /etc/passwd |sort -t ':' -k 3
root:x:0:0:root:/root:/bin/bash
cimsrvr:x:100:500:tog-pegasus OpenPegasus WBEM/CIM services:/var/lib/Pegasus:/sbin/nologin
luci:x:101:101::/var/lib/luci:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
將重復(fù)的數(shù)據(jù)僅列出一列
語法:uniq [-ic]
選項與參數(shù):
-i:忽略大小寫
-c:進行計數(shù)
舉例:
[root@localhost ~]# last|cut -d ' ' -f 1 |sort|uniq -c|sort -n
1 wtmp
3 tkf
26 reboot
84 root
=> last|cut -d ' ' -f 1 |sort 截取登錄名并排序
=> uniq -c 刪除重復(fù)列,并計數(shù)
=>sort –n 按照計數(shù)排序
wc 可以幫助我們統(tǒng)計文件字符信息
語法:wc [lwm]
選項與參數(shù):
-i:僅列出行
-w:僅列初子
-m:字符數(shù)
舉例:
[root@localhost ~]# wc /etc/man.config
141 722 4617 /etc/man.config
[root@localhost ~]# cat /etc/man.config |wc
141 722 4617
=>分別代表行數(shù),字數(shù),字符數(shù)
前面提到輸出數(shù)據(jù)流介質(zhì)可以是設(shè)備或文件,但數(shù)據(jù)流只能被一個介質(zhì)全部接收,那么如果希望數(shù)據(jù)可以被2個介質(zhì)接收就需要使用雙重數(shù)據(jù)流,簡單的說,如果你既想將輸出數(shù)據(jù)流保存到文件也想同時控制臺也會顯示,那你就需要使用這個了
語法:tee [-a] file
選項與參數(shù):-a:以累加的方式進行添加
舉例
[root@localhost ~]# export|tee export.list|cut -c 12-
COLORTERM="gnome-terminal"
DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-OeMZpvhP93,guid=30f2d841bcc5b92980611600531680a3"
DESKTOP_SESSION="default"
[root@localhost ~]# vim export.list
declare -x COLORTERM="gnome-terminal"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-OeMZpvhP93,guid=30f2d841bcc5b92980611600531680a3"
declare -x DESKTOP_SESSION="default"
tr 可以用來刪除和替換一些文字信息
說明,tr 只是改變輸出內(nèi)容,并不會真正去修改文件的內(nèi)容
語法:tr –d ‘字符’
tr –s ‘原字符’‘替換字符’
選項與參數(shù):
-d:刪除
-s:替換
舉例:
trfile 文件內(nèi)容
abcdefgh
abcdefgh
abcdefgh
[root@localhost ~]# cat trfile |tr -s 'b' 'B' =>替換
aBcdefgh
aBcdefgh
aBcdefgh
[root@localhost ~]# cat trfile |tr -d 'b' =>刪除
acdefgh
acdefgh
acdefgh
=>操作結(jié)束后 trfile 文件內(nèi)容不會有任何改變
col 主要將一些特殊字符進行轉(zhuǎn)換
語法:col [-xb]
選項與參數(shù):
-x:將 tab 鍵轉(zhuǎn)成相應(yīng)的空格
-b:在文字內(nèi)有反斜杠,僅保留反斜杠后面接的那個字符
舉例1:去掉反斜杠(^H)
[root@bogon ~]# man col > /root/col.man
[root@bogon ~]# cat -A /root/col.man|more
N^HNA^HAM^HME^HE$
c^Hco^Hol^Hl - filter reverse line feeds from input$
[root@bogon ~]# man col |col -b > /root/col.b.man
[root@bogon ~]# cat -A /root/col.b.man|more
col - filter reverse line feeds from input$
舉例2:將 TAB 轉(zhuǎn)換為空格 (^I)
[root@bogon ~]# cat -A /etc/man.config|more # MANPATH^I/opt/*/man$
# MANPATH^I/usr/lib/*/man$
# MANPATH^I/usr/share/*/man$
# MANPATH^I/usr/kerberos/man$
[root@bogon ~]# cat /etc/man.config |col -x > /root/man.tab.config
[root@bogon ~]# cat -A /root/man.tab.config|more
# MANBIN pathname$
# MANPATH manpath_element [corresponding_catdir]$
# MANPATH_MAP path_element manpath_element$
將[tab]按鍵轉(zhuǎn)為空格鍵
語法:expand [–t] file
選項與參數(shù):
-t:[tab] 按鍵替換多少個空格字符
舉例
[root@localhost ~]# grep '^MANPATH' /etc/man.config |head -n 3|cat -A
MANPATH^I/usr/man$
MANPATH^I/usr/share/man$
MANPATH^I/usr/local/man$
[root@localhost ~]# grep '^MANPATH' /etc/man.config |head -n 3|expand -6|cat -A
MANPATH /usr/man$
MANPATH /usr/share/man$
MANPATH /usr/local/man$
語法:split [-bl] file PREFIX
選項與參數(shù):
-b:后面可接欲切割的文件大小
-1:以行數(shù)進行切割
PREFIX:切割后文件的前導(dǎo)符
舉例1:切割文件
[root@localhost ~]# ll -h /etc/termcap
-rw-r--r-- 1 root root 789K 2007-01-07 /etc/termcap
[root@localhost ~]# split -b 300k /etc/termcap newter
[root@localhost ~]# ll -h newter*
-rw-r--r-- 1 root root 300K 03-06 09:56 newteraa
-rw-r--r-- 1 root root 300K 03-06 09:56 newterab
-rw-r--r-- 1 root root 189K 03-06 09:56 newterac
舉例2 :合成文件
[root@localhost ~]# cat newter* >> termap-back
[root@localhost ~]# ll -h termap-back
-rw-r--r-- 1 root root 789K 03-06 10:02 termap-back
參數(shù)代換的作用:
1.作為某些指令的參數(shù)。比如 which, finger ,find ,whereis 等
2.作為某些不支持管道命令的輸入數(shù)據(jù)流
語法:xargs [-epn] command
選項與參數(shù):
-e:就是 EOF 的意思,后面可接一個字符串,當分析到這個字符串時,就會停止繼續(xù)工作
-p:在執(zhí)行每個參數(shù)時,都會詢問用戶
-n:后面接次數(shù),執(zhí)行 command 的次數(shù)
舉例1:指令參數(shù)
[root@localhost ~]# cat ./xargsfile
cd
ll
grep
[root@localhost ~]# cut -d ' ' -f 1 ./xargsfile|xargs whereis
cd: /usr/share/man/man1p/cd.1p.gz /usr/share/man/man1/cd.1.gz
ll:
grep: /bin/grep /usr/share/man/man1p/grep.1p.gz /usr/share/man/man1/grep.1.gz
舉例2:作為輸入數(shù)據(jù)流
[root@localhost ~]# find /sbin/ -perm +7000|ls -l
總計 227644
-rw------- 1 root root 1377 02-14 10:29 anaconda-ks.cfg
drwxr-xr-x 2 root root 4096 02-21 13:30 Desktop
…….
=>ls 不支持管道命令,一次查詢的結(jié)果是~/下的內(nèi)容
[root@localhost ~]# find /sbin/ -perm +7000|xargs ls -l
-rwsr-xr-x 1 root root 73108 10-02 05:10 /sbin/mount.nfs
-rwsr-xr-x 1 root root 73112 10-02 05:10 /sbin/mount.nfs4
…..
=>將 find 查詢到的內(nèi)容作為輸入數(shù)據(jù)流供 ls 使用