Linux中的管道命令(一)

cat

cat程序?qū)?shù)據(jù)不加改變的復(fù)制到標(biāo)準(zhǔn)輸出,數(shù)據(jù)可以來(lái)自于標(biāo)注輸入,也可以來(lái)自于文件。

下面的程序?qū)⑤敵?code>hello.txt文件的內(nèi)容:

$ cat hello.txt

cat命令常被用作組合多個(gè)文件,下面的命令將hello.txtsource.list.bk文件的內(nèi)容組合保存到bk.txt中:

$ cat hello.txt source.list.bk > bk.txt

split

使用cat可以組合文件,使用split可以分割文件。

split默認(rèn)將文件每1000行存為一個(gè)新文件,如果最后一次分割后剩余的不足1000行,則將剩余的行作為一個(gè)文件,也可以使用-l命令選項(xiàng)指定每個(gè)新文件的行數(shù):

$ split -l 4 bk.txt

上面的命令將bk.txt文件每4行分割為一個(gè)新文件,bk.txt文件共15行,因此生成4個(gè)文件(前三個(gè)每個(gè)都是4行,最后一個(gè)是3行),其名字依次為xaa、xab、xacxad。

split生成的新文件的默認(rèn)名字為xaaxab、xac……,使用-d選項(xiàng)指定使用數(shù)字作為新文件名后綴,默認(rèn)是兩位數(shù)字,還可以在文件名后面指定新文件的文件名前綴:

$ split -d -l 4 bk.txt source

生成的四個(gè)文件的文件名為source00source01、source02source03

使用-a選項(xiàng)指定數(shù)字或者字母后綴的位數(shù),下面的命令指定使用數(shù)字后綴且數(shù)字位數(shù)為3位:

$ split -d -a 3 -l 4 bk.txt source_

生成的四個(gè)文件的文件名依次是:source_000source_001source_002source_003。

tac

tac逆序輸出每一行:

$ cat output
li
qian
sun
wang
wu
zhao
zheng
zhou
$ tac output
zhou
zheng
zhao
wu
wang
sun
qian
li

rev

rev對(duì)每一行的內(nèi)容進(jìn)行逆序輸出:

$ cat output
li
qian
sun
wang
wu
zhao
zheng
zhou
$ cat output | tac
zhou
zheng
zhao
wu
wang
sun
qian
li
$ cat output | tac | rev
uohz
gnehz
oahz
uw
gnaw
nus
naiq
il

head, tail

head默認(rèn)顯示文件的前10行內(nèi)容,tail默認(rèn)顯示文件的最后10行內(nèi)容。

它們都可以使用-n指定顯示的行數(shù):

$ head -n 2 bk.txt
Hello, vim!ello
Hello, vim!Hello
$ tail -n 5 bk.txt
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse

cut

cat命令在每行信息中進(jìn)行切割。

下面是$PATH變量的內(nèi)容:

$ echo $PATH
/home/tom/bin:/home/tom/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

在該變量中,每一個(gè)目錄項(xiàng)由:進(jìn)行分割,第1個(gè)目錄項(xiàng)是/home/tom/bin,第2個(gè)目錄項(xiàng)是/home/tom/.local/bin……

cut命令使用-d '字符'選項(xiàng)即可使用指定的字符進(jìn)行切分,使用-f選項(xiàng)即可指定分割后要保留的部分:

$ echo $PATH | cut -d ':' -f 1,3
/home/tom/bin:/usr/local/sbin

上面的語(yǔ)句中,cut使用:對(duì)$PATH變量進(jìn)行了切分,并且保留了其第1和第3項(xiàng)。

對(duì)于下面這樣很“整齊”的文件:

$ cat source.list.bk
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse

還可以對(duì)cut使用-c命令選項(xiàng)指定截取的字符,比如cut -c 2-4表示截取每行的第2-4個(gè)字符。下面的命令截取每行的39至最后一個(gè)字符:

$ cat source.list.bk | cut -c 39-
xenial main restricted universe multiverse
xenial-security main restricted universe multiverse
xenial-updates main restricted universe multiverse
xenial-proposed main restricted universe multiverse
xenial-backports main restricted universe multiverse
?著作權(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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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