参数代换命令xargs使用小结

        我们可以用管道将一个命令的stdout重定向到另一个命令的stdin。例如: cat test.txt | grep 'ab' ,但是有些命令只能以命令行参数的形式接受参数,而无法通过stdin接受数据流。xargs可以解决这种问题,xargs可以读入stdin的数据,并且以空格或断行符进行分辨,将stdin的数据分隔成某个命令的参数。xargs可以将单行或多行文本输入转换成其他格式。

       用法: xargs [-0epnd] command

       参数:

                  -0: 如果输入的stdin含有特殊字符,例如`, \, 空格键等字符时,这个参数可以将它还原成一般字符。这个参数可以用于特殊状态。

                  -e:这个是EOF(end of file)的意思。后面可以接一个字符串,当xargs分析到这个字符串时,就会停止工作。

                  -p:在执行每个命令的参数时,都会询问用户。

                  -n:后面接次数,每次command命令执行时,要使用几个参数。

                  -d:使用自己的定义的定界符来分隔参数。

                   -I:大写i,将xargs的每项名称,一般是一行一行赋值给{},可以{}代替。使用-i的时候,命令以循环的方式执行。如果有3个参数,

                          那么命令就会连同{}一起被执行3次。在每一次执行中{}都会被替换为相应的参数。

                         

      范例:

               例1:使用-0选项删除test文件夹中文件名含空格的文件"file 1.log" "file 2.log"          

[root@centos6 test]# touch "file 1.log" "file 2.log"
[root@centos6 test]# ls -l *.log
-rw-r--r--. 1 root root 0 Jun 16 18:57 file 1.log
-rw-r--r--. 1 root root 0 Jun 16 18:57 file 2.log
[root@centos6 test]# find -name '*.log' | xargs rm  #直接删除不成功,xargs默认是以空白字符(空格、TAB、换行符)
来分割记录
rm: cannot remove `./file': No such file or directory
rm: cannot remove `2.log': No such file or directory
rm: cannot remove `./file': No such file or directory
rm: cannot remove `1.log': No such file or directory
[root@centos6 test]# find -name '*.log' -print0 | xargs -0  rm  # find在打印出一个文件名之后接着输出一个NULL字符,
然后告诉xargs用NULL字符来作为记录的分隔
[root@centos6 test]# find -name '*.log'

              例2:使用-e选项只打印”a bc d e rf f"中字符e之前的字符串

[root@centos6 test]# echo a bc d e rf f | xargs -ee
a bc d


             例3:使用-p选项

[root@centos6 test]# ls a* | xargs -p rm  # 执行rm前,先询问下
rm a1 a2 a3 ?...y
[root@centos6 test]# ls a*
ls: cannot access a*: No such file or directory

            例4:使用-n选项批量创建user1,user2,user3三个用户

[root@centos6 test]# echo user1 user2 user3 | xargs -n 1 useradd 
[root@centos6 test]# cat /etc/passwd | tail -3
user1:x:708:726::/home/user1:/bin/bach
user2:x:709:727::/home/user2:/bin/bach
user3:x:710:728::/home/user3:/bin/bach
           例5:将多行输入转换成单行输出

[root@centos6 test]# cat example.txt 
1 2 3 4 5 6
7 8 9 10
11 12
[root@centos6 test]# cat example.txt | xargs 
1 2 3 4 5 6 7 8 9 10 11 12
           例6:将单行输入转换成多行输出
[root@centos6 test]# cat example.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
10 11 12
           例7:使用-d选项分隔字符串成单行或多行

[root@centos6 test]# echo "splitXsplitXsplitXsplit" | xargs -d X
split split split split
[root@centos6 test]# echo "splitXsplitXsplitXsplit" | xargs -d X -n 2  # 结合-n选项使用
split split
split split

 

           例8:使用-I(大写i)

[root@centos6 xargsi]# echo This is file1.txt > file1.txt
[root@centos6 xargsi]# echo This is file2.txt > file2.txt
[root@centos6 xargsi]# echo This is file3.txt > file3.txt
[root@centos6 xargsi]# vim files.txt 
[root@centos6 xargsi]# cat files.txt 
file1.txt
file2.txt
file3.txt
[root@centos6 xargsi]# cat files.txt | xargs -I {} cat {}   等价于cat files.txt | ( while read arg ; do cat $arg; done )
This is file1.txt
This is file2.txt
This is file3.txt
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值