(1)将前一个命令的标准输出传递给下一个命令,作为它的参数,xargs的默认命令是echo,空格是默认定界符
(2)将多行输入转换为单行
front command | xargs -option later command
front command:前一个命令
-option:xargs的选项
later command:后一个命令
-n:指定一次处理参数的个数 -d:自定义参数界定符 -p:询问是否允许later command 参数 -t:表示先打印命令,然后再执行 -i:逐项处理
其他参考:man xargs
测试文本: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
(1)多行输入单行输出
cat test.txt | xargs
结果:
a b c d e f g h i j k l m n o p q r t s u v w x y z
(2)指定一次处理的参数个数:指定为5,多行输出
cat test.txt |xargs -n 5
结果:
a b c d e
f g h i j
k l m n o
p q r t s
u v w x y
z
(3)自定义参数界定符:指定界定符为‘m’
cat test.txt | xargs -d m
结果:
a b c d e f g
h i j k l n
o p q
r s t
u v w x y z
(4)询问是否运行later command 参数
cat test.txt | xargs -n 5 -p
结果:
(5)删除指定名称的进程:进程名为Deadpool
ps -ef | grep ‘Deadpool‘ | grep -v ‘grep‘| awk ‘{print $2}‘ | xargs kill -9
原文:https://www.cnblogs.com/xiaohaigegede/p/13632838.html