Linux中的Xargs

xargs从标准输入(stdin)中读取数据进行处理

  • 数据以空格进行分割
  • 可以根据参数进行一次或多次处理,默认的处理命令是/bin/echo
  • 空号不进行处理,会被忽略
  • 遇到命令状态为255时,xargs会立刻停止,譬如发生错误时。
    下面是xargs包含的参数

1、“-a”从文件中读取数据

[root@Simile /]# cat test.txt
a a b b c c
The new line
Brand new me
[root@Simile /]# xargs -a test.txt
a a b b c c The new line Brand new me

2、‘-0’当输入有特殊字符时,将其当做一般字符处理,比如“”和空格

暂时不太理解这个的具体用处

3、‘-d’指定分隔符

[root@Simile /]# cat line.txt
AiBiCiD
[root@Simile /]# cat line.txt | xargs -d'i'
A B C D

4、‘-E eof-str’指定结束标志为eof-str,xargs处理到这个标志就会停止
※需要注意的是:该用法只会检查空格分开的段落,不会检查单个字符。

[root@Simile /]# cat line.txt
Ai BiCi Di
The new line
[root@Simile /]# xargs -E'i' -a line.txt
Ai BiCi Di The new line #此处用字符‘i’进行检索,并未生效
[root@Simile /]# xargs -E'new' -a line.txt
Ai BiCi Di The

5、‘-I replace-str’将每一行输入,输入内容替换为 replace-str
line.txt有2行数据,获取该数据后,用echo命令写入到line中

[root@Simile /]# cat line.txt 
Ai BiCi Di
The new line
[root@Simile /]# cat line.txt |xargs -t -I {} echo {}>>line.txt
echo Ai BiCi Di 
echo The new line 
[root@Simile /]# cat line.txt 
Ai BiCi Di
The new line
Ai BiCi Di
The new line

6、‘-i’同上
7、‘-n max-args’每次执行max-args个输入,默认值会执行所有

[root@Simile /]# cat line.txt
Ai BiCi Di
The new line
a is char
[root@Simile /]# cat line.txt |xargs -n 2
Ai BiCi
Di The
new line
a is
char
[root@Simile /]# cat line.txt |xargs -n 4
Ai BiCi Di The
new line a is
char

8、‘-p’交互模式。执行前询问,y执行,n不执行。

[root@Simile /]# cat line.txt |xargs -p
/bin/echo Ai BiCi Di The new line a is char ?...y
Ai BiCi Di The new line a is char
[root@Simile /]# cat line.txt |xargs -p
/bin/echo Ai BiCi Di The new line a is char ?...n

9、‘-r’无输入对象不会执行,

[root@Simile /]# echo ""|xargs -t mv
mv 
mv: missing file operand
Try `mv --help' for more information.
[root@Simile /]# echo ""|xargs -t -r mv

10、‘-s max-chars’xargs每次执行命令的最大长度。
※可以自己试验一下。

11、‘-x’当xargs执行的命令长度大于-s max-char时,停止执行

12、‘-P max-procs’修改线程数,默认为单线程.max-procs为0时,as many processes as possible

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值