學習Linux命令之 expand split xargs


expand


# expand [-t] file

        將 [tab] 按鍵轉成空格鍵

選項與參數:

-t     :後面可以接數字。一般來說,一個 tab 按鍵可以用 8 個空格鍵取代。


root@localhost:~/shell# cat file1

John Dagget341 King RoadPlymouth

Alice Ford22 East BroadwayRicahmond

root@localhost:~/shell# expand -t 4 file1

John Dagget 341 King Road   Plymouth

Alice Ford  22 East Broadway    Ricahmond

root@localhost:~/shell# expand -t 8 file1

John Dagget     341 King Road   Plymouth

Alice Ford      22 East Broadway        Ricahmond

root@localhost:~/shell# expand -t 10 file1

John Dagget         341 King Road       Plymouth

Alice Ford          22 East Broadway    Ricahmond



split

        

[root@www ~]# split [-bl] file PREFIX

        依據文件大小或行數來分割,就可以將大文件分割成爲小文件

選項與參數:

-b :後面可接欲分割成的文件大小,可加單位,例如 b, k, m 等;

-l :以行數來進行分割。

PREFIX :代表前導符的意思,可作爲分割文件的前導文字。


root@localhost:~/shell# ll awk 

-rw-r--r-- 1 root Ricardo 40539  5月  8 20:30 awk

root@localhost:~/shell# split -b 15k awk awk  〈--前導文字

root@localhost:~/shell# ll awk*

-rw-r--r-- 1 root Ricardo 40539  5月  8 20:30 awk

-rw-r--r-- 1 root Ricardo 15360  5月  8 20:33 awkaa

-rw-r--r-- 1 root Ricardo 15360  5月  8 20:33 awkab

-rw-r--r-- 1 root Ricardo  9819  5月  8 20:33 awkac


root@localhost:~/shell# split -l 400 awk ssk

root@localhost:~/shell# ll ssk*

-rw-r--r-- 1 root Ricardo 17625  5月  8 20:34 sskaa

-rw-r--r-- 1 root Ricardo 18095  5月  8 20:34 sskab

-rw-r--r-- 1 root Ricardo  4819  5月  8 20:34 sskac


xargs

1. 簡介

之所以能用到這個命令,關鍵是由於很多命令不支持|管道來傳遞參數,而日常工作中有有這個必要,

所以就有了 xargs 命令,例如:

find /sbin -perm +700 |ls -l

這個命令是錯誤的

find /sbin -perm +700 |xargs ls -l 這樣纔是正確的

xargs 可以讀入 stdin 的資料,並且以空白字元或斷行字元作爲分辨,將 stdin 的資料分隔成爲 arguments。 因爲是以空白字元作爲分隔,所以,如果有一些檔名或者是其他意義的名詞內含有空白字元的時候, xargs 可能就會誤判。


2. 選項解釋


-0 當 sdtin 含有特殊字元時候,將其當成一般字符,想/'空格等

root@localhost:~/shell# echo "\\"

\

root@localhost:~/shell# echo "\\" | xargs echo



root@localhost:~/shell# echo "\\" | xargs -0 echo

\



-a file 從文件中讀入作爲 sdtin


root@localhost:~/shell# cat test

John Dagget, 341 King Road, Plymouth, Massachusetts

Alice Ford, 22 East Broadway, Ricahmond, Virginia

root@localhost:~/shell# xargs -a test echo

John Dagget, 341 King Road, Plymouth, Massachusetts Alice Ford, 22 East Broadway, Ricahmond, Virginia


-E flag ,注意有的時候可能會是-e flag 必須是一個以空格分隔的標誌,當 xargs 分析到含有 flag 這個

標誌的時候就停止。


root@localhost:~/shell# cat test | xargs -E 'King' echo

John Dagget, 341


-p 當每次執行一個 argument 的時候詢問一次用戶。


root@localhost:~/shell# cat test | xargs -p echo

echo John Dagget, 341 King Road, Plymouth, Massachusetts ?...

echo Alice Ford, 22 East Broadway, Ricahmond, Virginia ?...


-n num 後面加次數,表示命令在執行的時候一次用的 argument 的個數,默認是用所有的。


root@localhost:~/shell# cat help 

test

yes 

no 

sd 

df

root@localhost:~/shell# cat help | xargs -i -n2 echo 

test yes

no sd

df


-t 表示先打印命令,然後再執行。


root@localhost:~/shell# cat help | xargs -t -i -n2 echo 

echo test yes 

test yes

echo no sd 

no sd

echo df 

df


-i 或者是-I,這得看 linux 支持了,將 xargs 的每項名稱,一般是一行一行賦值給{},可以用{}代替。


root@localhost:~/shell# ls test*

test

root@localhost:~/shell# ls test| xargs -t -i mv {} {}.bak

mv test test.bak 

root@localhost:~/shell# ls tes*

test.bak


-d delim 分隔符,默認的 xargs 分隔符是回車,argument 的分隔符是空格,這裏修改的是 xargs 的分隔


root@localhost:~/shell# cat help 

add rs rc sds

root@localhost:~/shell# cat help | xargs -i -p -d " " echo {}

echo add ?...y

echo rs ?...add

y

echo rc ?...rs

y

echo sds

 ?...rc

y

sds



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章