天天看點

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
           

繼續閱讀