1. 程式人生 > >linux xargs命令用法

linux xargs命令用法

xargs命令:讀取輸入資料重新格式化後輸出,將標準輸入資料轉換成命令列引數輸出。


定義一個測試檔案:

[[email protected] study]$ cat test.txt 
a b c d e
f g h i j k
l m n o p q r
s t u v w x y z


xargs預設命令是echo,空格是預設定界符。預設情況下,多行輸入通過xargs實現了單行輸出。

[[email protected] study]$ cat test.txt | xargs
a b c d e f g h i j k l m n o p q r s t u v w x y z


-n 選項:實現多行輸出,-n指定每行輸出的個數

[[email protected] study]$ cat test.txt | xargs -n3 
a b c
d e f
g h i
j k l
m n o
p q r
s t u
v w x
y z


-d 選項:自定義一個定界符,通過指定定界符把輸入隔開

[[email protected] study]$ echo "hahaOhahaOhahaOhaha" | xargs -dO 
haha haha haha haha


-I 選項:使用-I指定一個替換字串{},這個字串在xargs擴充套件時會被替換掉,當-I與xargs結合使用,每一個引數命令都會被執行一次。也就是說對於前面輸出的每一個數據都會被作為後面的引數使用,即輸出的aaa會作為echo hello的引數使用。

[[email protected] study]$ echo -e "aaa\nbbb\nccc" | xargs -I {} echo hello {}
hello aaa
hello bbb
hello ccc