参数代换命令xargs使用小结
来源:互联网 发布:示波器y轴输入端口 编辑:程序博客网 时间:2024/06/06 09:41
我们可以用管道将一个命令的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 directoryrm: cannot remove `2.log': No such file or directoryrm: cannot remove `./file': No such file or directoryrm: 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 -eea 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 -3user1:x:708:726::/home/user1:/bin/bachuser2:x:709:727::/home/user2:/bin/bachuser3:x:710:728::/home/user3:/bin/bach例5:将多行输入转换成单行输出
[root@centos6 test]# cat example.txt 1 2 3 4 5 67 8 9 1011 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 31 2 34 5 67 8 910 11 12例7:使用-d选项分隔字符串成单行或多行
[root@centos6 test]# echo "splitXsplitXsplitXsplit" | xargs -d Xsplit split split split[root@centos6 test]# echo "splitXsplitXsplitXsplit" | xargs -d X -n 2 # 结合-n选项使用split splitsplit 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.txtfile2.txtfile3.txt[root@centos6 xargsi]# cat files.txt | xargs -I {} cat {} 等价于cat files.txt | ( while read arg ; do cat $arg; done )This is file1.txtThis is file2.txtThis is file3.txt
- 参数代换命令xargs使用小结
- Linux xargs参数代换命令
- linux命令之参数代换:xargs
- 参数代换:xargs
- Linux命令备忘实例(9)——参数代换xargs
- Linux xargs命令 小结
- 排序命令&参数代换args
- shell 中find和xargs命令使用小结
- find命令的xargs参数的使用---Linux学习笔记
- Linux:使用xargs为其他命令提供参数
- linux xargs命令使用
- 命令代换
- 命令代换
- 命令代换
- Linux xargs命令的使用
- xargs命令作用以及参数详情
- xargs小结
- xargs可以将一个命令的输出作为另一个命令的参数——参看实例的使用
- [SCOI2005] BZOJ 1088 扫雷-状压dp
- 子进程的信号捕捉函数是否会因为另外一个子进程的结束而被触发
- scikit-learn工具包使用建议(转)
- bom查询
- python numpy、lxml等库安装总是安装失败,啊哈哈哈哈哈哈我这次知道了一个神奇的软件!
- 参数代换命令xargs使用小结
- 相关搜索词的一种粗排方法
- Android ijkplayer的使用解析
- Android HttpURLConnection服务器POST上传数据
- 斐波那契数列
- H5离线缓存
- 约瑟夫问题
- butterknife框架完整导入
- JPA 基本属性注解