cut

1 一兩句話描述一下cut命令吧!

正如其名,cut的工作就是“剪”,具體的說(shuō)就是在文件中負(fù)責(zé)剪切數(shù)據(jù)用的。

cut是以每一行為一個(gè)處理對(duì)象的,這種機(jī)制和sed是一樣的。(關(guān)于sed的入門文章將在近期發(fā)布)

2 cut一般以什么為依據(jù)呢? 也就是說(shuō),我怎么告訴cut我想定位到的剪切內(nèi)容呢?

cut命令主要是接受三個(gè)定位方法:

第一,字節(jié)(bytes),用選項(xiàng)-b

第二,字符(characters),用選項(xiàng)-c

第三,域(fields),用選項(xiàng)-f

3 以“字節(jié)”定位,給個(gè)最簡(jiǎn)單的例子?

舉個(gè)例子吧,當(dāng)你執(zhí)行ps命令時(shí),會(huì)輸出類似如下的內(nèi)容:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ who
rocrocket :0 2009-01-08 11:07
rocrocket pts/0 2009-01-08 11:23 (:0.0)
rocrocket pts/1 2009-01-08 14:15 (:0.0)</pre>

如果我們想提取每一行的第3個(gè)字節(jié),就這樣:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ who|cut -b 3
c
c
c</pre>

看明白了吧,-b后面可以設(shè)定要提取哪一個(gè)字節(jié),其實(shí)-b和3之間沒有空格也是可以的,但推薦有空格:)

4 如果“字節(jié)”定位中,我想提取第3,第4、第5和第8個(gè)字節(jié),怎么辦?

-b支持形如3-5的寫法,而且多個(gè)定位之間用逗號(hào)隔開就成了。看看例子吧:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ who|cut -b 3-5,8
croe
croe
croe</pre>

但有一點(diǎn)要注意,cut命令如果使用了-b選項(xiàng),那么執(zhí)行此命令時(shí),cut會(huì)先把-b后面所有的定位進(jìn)行從小到大排序,然后再提取。可不能顛倒定位的順序哦。這個(gè)例子就可以說(shuō)明這個(gè)問題:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ who|cut -b 8,3-5
croe
croe
croe</pre>

5 還有哪些類似“3-5”這樣的小技巧,列舉一下吧!

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ who
rocrocket :0 2009-01-08 11:07
rocrocket pts/0 2009-01-08 11:23 (:0.0)
rocrocket pts/1 2009-01-08 14:15 (:0.0)
[rocrocket@rocrocket programming]$ who|cut -b -3
roc
roc
roc
[rocrocket@rocrocket programming]$ who|cut -b 3-
crocket :0 2009-01-08 11:07
crocket pts/0 2009-01-08 11:23 (:0.0)
crocket pts/1 2009-01-08 14:15 (:0.0)</pre>

想必你也看到了,-3表示從第一個(gè)字節(jié)到第三個(gè)字節(jié),而3-表示從第三個(gè)字節(jié)到行尾。如果你細(xì)心,你可以看到這兩種情況下,都包括了第三個(gè)字節(jié)“c”。
如果我執(zhí)行who|cut -b -3,3-,你覺得會(huì)如何呢?答案是輸出整行,不會(huì)出現(xiàn)連續(xù)兩個(gè)重疊的c的??矗?/p>

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ who|cut -b -3,3-
rocrocket :0 2009-01-08 11:07
rocrocket pts/0 2009-01-08 11:23 (:0.0)
rocrocket pts/1 2009-01-08 14:15 (:0.0)</pre>

6 給個(gè)以字符為定位標(biāo)志的最簡(jiǎn)單的例子吧!

下面例子你似曾相識(shí),提取第3,第4,第5和第8個(gè)字符:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ who|cut -c 3-5,8
croe
croe
croe</pre>

不過,看著怎么和-b沒有什么區(qū)別?。磕?b和-c作用一樣? 其實(shí)不然,看似相同,只是因?yàn)檫@個(gè)例子舉的不好,who輸出的都是單字節(jié)字符,所以用-b和-c沒有區(qū)別,如果你提取中文,區(qū)別就看出來(lái)了,來(lái),看看中文提取的情況:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ cat cut_ch.txt
星期一
星期二
星期三
星期四
[rocrocket@rocrocket programming]$ cut -b 3 cut_ch.txt
?
?
?
?
[rocrocket@rocrocket programming]$ cut -c 3 cut_ch.txt



四</pre>

看到了吧,用-c則會(huì)以字符為單位,輸出正常;而-b只會(huì)傻傻的以字節(jié)(8位二進(jìn)制位)來(lái)計(jì)算,輸出就是亂碼。
既然提到了這個(gè)知識(shí)點(diǎn),就再補(bǔ)充一句,如果你學(xué)有余力,就提高一下。
當(dāng)遇到多字節(jié)字符時(shí),可以使用-n選項(xiàng),-n用于告訴cut不要將多字節(jié)字符拆開。例子如下:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ cat cut_ch.txt |cut -b 2
?
?
?
?
[rocrocket@rocrocket programming]$ cat cut_ch.txt |cut -nb 2

[rocrocket@rocrocket programming]$ cat cut_ch.txt |cut -nb 1,2,3



星</pre>

6 域是怎么回事呢?解釋解釋:)

為什么會(huì)有“域”的提取呢,因?yàn)閯偛盘岬降?b和-c只能在固定格式的文檔中提取信息,而對(duì)于非固定格式的信息則束手無(wú)策。這時(shí)候“域”就派上用場(chǎng)了。

(下面的講解內(nèi)容是在假設(shè)你對(duì)/etc/passwd文件的內(nèi)容和組織形式比較了解的情況下進(jìn)行的。)

如果你觀察過/etc/passwd文件,你會(huì)發(fā)現(xiàn),它并不像who的輸出信息那樣具有固定格式,而是比較零散的排放。但是,冒號(hào)在這個(gè)文件的每一行中都起到了非常重要的作用,冒號(hào)用來(lái)隔開每一個(gè)項(xiàng)。

我們很幸運(yùn),cut命令提供了這樣的提取方式,具體的說(shuō)就是設(shè)置“間隔符”,再設(shè)置“提取第幾個(gè)域”,就OK了!

以/etc/passwd的前五行內(nèi)容為例:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ cat /etc/passwd|head -n 5
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[rocrocket@rocrocket programming]$ cat /etc/passwd|head -n 5|cut -d : -f 1
root
bin
daemon
adm
lp</pre>

看到了吧,用-d來(lái)設(shè)置間隔符為冒號(hào),然后用-f來(lái)設(shè)置我要取的是第一個(gè)域,再按回車,所有的用戶名就都列出來(lái)了!呵呵 有成就感吧!
當(dāng)然,在設(shè)定-f時(shí),也可以使用例如3-5或者4-類似的格式:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ cat /etc/passwd|head -n 5|cut -d : -f 1,3-5
root:0:0:root
bin:1:1:bin
daemon:2:2:daemon
adm:3:4:adm
lp:4:7:lp
[rocrocket@rocrocket programming]$ cat /etc/passwd|head -n 5|cut -d : -f 1,3-5,7
root:0:0:root:/bin/bash
bin:1:1:bin:/sbin/nologin
daemon:2:2:daemon:/sbin/nologin
adm:3:4:adm:/sbin/nologin
lp:4:7:lp:/sbin/nologin
[rocrocket@rocrocket programming]$ cat /etc/passwd|head -n 5|cut -d : -f -2
root:x
bin:x
daemon:x
adm:x
lp:x</pre>

7 如果遇到空格和制表符時(shí),怎么分辨呢?我覺得有點(diǎn)亂,怎么辦?

有時(shí)候制表符確實(shí)很難辨認(rèn),有一個(gè)方法可以看出一段空格到底是由若干個(gè)空格組成的還是由一個(gè)制表符組成的。

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ cat tab_space.txt
this is tab finish.
this is several space finish.
[rocrocket@rocrocket programming]$ sed -n l tab_space.txt
this is tab\tfinish.$
this is several space finish.$</pre>

看到了吧,如果是制表符(TAB),那么會(huì)顯示為\t符號(hào),如果是空格,就會(huì)原樣顯示。
通過此方法即可以判斷制表符和空格了。
注意,上面sed -n后面的字符是L的小寫字母哦,不要看錯(cuò)。(字母l、數(shù)字1還有或運(yùn)算|真是難分辨啊…,看來(lái)這三個(gè)比制表符還難分辨…)

8 我應(yīng)該在cut -d中用什么符號(hào)來(lái)設(shè)定制表符或空格呢?

悄悄的告訴你,cut的-d選項(xiàng)的默認(rèn)間隔符就是制表符,所以當(dāng)你就是要使用制表符的時(shí)候,完全就可以省略-d選項(xiàng),而直接用-f來(lái)取域就可以了!放心,相信我!
如果你設(shè)定一個(gè)空格為間隔符,那么就這樣:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ cat tab_space.txt |cut -d ' ' -f 1
this
this</pre>

注意,兩個(gè)單引號(hào)之間可確實(shí)要有一個(gè)空格哦,不能偷懶。
而且,你只能在-d后面設(shè)置一個(gè)空格,可不許設(shè)置多個(gè)空格,因?yàn)閏ut只允許間隔符是一個(gè)字符。

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ cat tab_space.txt |cut -d ' ' -f 1
cut: the delimiter must be a single character
Try `cut --help' for more information.</pre>

9 我想將ps和cut命令配合使用時(shí),怎么總是在最后兩行出現(xiàn)重復(fù)現(xiàn)象?

這個(gè)問題的具體描述是如下這樣的。
當(dāng)cut和ps配合時(shí):

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ ps
PID TTY TIME CMD
2977 pts/0 00:00:00 bash
5032 pts/0 00:00:00 ps
[rocrocket@rocrocket programming]$ ps|cut -b3
P
9
0
0</pre>

看,最后的0重復(fù)了兩次?。《?,我也試過ps ef或ps aux均有此問題。

而當(dāng)ps和其他命令配合時(shí),均無(wú)此問題,例如cut和who配合則正常:

<pre lang="bash" style="box-sizing: inherit; overflow: auto; font-family: "Courier 10 Pitch", Courier, monospace; font-size: 0.9375rem; background: rgb(238, 238, 238); line-height: 1.6; margin-bottom: 1.6em; max-width: 100%; padding: 1.6em; color: rgb(64, 64, 64); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: justify; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">[rocrocket@rocrocket programming]$ who
rocrocket :0 2009-01-08 11:07
rocrocket pts/0 2009-01-08 11:23 (:0.0)
rocrocket pts/1 2009-01-08 14:15 (:0.0)
[rocrocket@rocrocket programming]$ who|cut -b3
c
c
c</pre>

這個(gè)看似怪異的令我百思不得其解的問題,得到了sunway的解答,在此非常感謝他。我發(fā)問的原帖地址在[此處]
其實(shí)這個(gè)問題是這樣的,ps|cut會(huì)自身創(chuàng)建一個(gè)進(jìn)程,所以當(dāng)ps時(shí)也會(huì)提取出這個(gè)進(jìn)程,然后通過管道輸出到cut,所以cut截取后,就多出了一行,之所以會(huì)重復(fù)上一行內(nèi)容,是由于我們恰巧取到了和上一行內(nèi)容相同的字符而已。
你測(cè)試下執(zhí)行ps和ps|cat就知道原因了!:)

10 cut有哪些缺陷和不足?

猜出來(lái)了吧?對(duì),就是在處理多空格時(shí)。
如果文件里面的某些域是由若干個(gè)空格來(lái)間隔的,那么用cut就有點(diǎn)麻煩了,因?yàn)閏ut只擅長(zhǎng)處理“以一個(gè)字符間隔”的文本內(nèi)容。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、定義 正如其名,cut的工作就是“剪”,具體的說(shuō)就是在文件中負(fù)責(zé)剪切數(shù)據(jù)用的。cut是以每一行為一個(gè)處理對(duì)象的...
    隨風(fēng)化作雨閱讀 375評(píng)論 0 0
  • 本文轉(zhuǎn)自linux命令5分鐘系列 cut是一個(gè)選取命令,就是將一段數(shù)據(jù)經(jīng)過分析,取出我們想要的。一般來(lái)說(shuō),選取信息...
    井底蛙蛙呱呱呱閱讀 1,704評(píng)論 0 2
  • cut是以每一行為一個(gè)處理對(duì)象的。cut一般以什么為依據(jù)呢? 也就是說(shuō),我怎么告訴cut我想定位到的剪切內(nèi)容呢? ...
    Viking_Den閱讀 21,995評(píng)論 0 3
  • cut 打印輸出文本文件每行的特定范圍內(nèi)容 cut -b/-c list [-n] [file...] -b li...
    Wavky閱讀 652評(píng)論 0 0
  • 給大家講一個(gè)故事,是有關(guān)欲望的故事。 他,一個(gè)人孤單的躺在沙漠里,靜靜的,等待著死亡的降臨。 幾天前,有人告訴他,...
    榮若言閱讀 262評(píng)論 0 0

友情鏈接更多精彩內(nèi)容