- [root@CentOS5 shell]# cat a.txt
- hello world
- oh my god
- [root@CentOS5 shell]# cat a.txt | xargs
- hello world oh my god
- [root@CentOS5 shell]# cat a.txt
- hello world
- oh my god
- [root@CentOS5 shell]# cat a.txt | xargs -n 1
- hello
- world
- oh
- my
- god
- [root@CentOS5 shell]# cat a.txt | xargs -n 2
- hello world
- oh my
- god
3.分割字符串
- [root@CentOS5 shell]# echo "123;abc;xyz;456"| xargs -d ';'
- 123 abc xyz 456
- [root@CentOS5 shell]# echo "123;abc;xyz;456"| xargs -d ';' -n 1
- 123
- abc
- xyz
- 456
- [root@CentOS5 shell]# echo "123;abc;xyz;456"| awk -F ';' 'END{for(i=1;i<=NF;i++)print $i}'
- 123
- abc
- xyz
- 456
- [root@CentOS5 shell]# echo "123;abc;xyz;456"| sed 's/;/\n/g'
- 123
- abc
- xyz
- 456
4.通过xargs批量传参
如,批量创建文件
- [root@CentOS5 shell]# ls
- a.txt
- [root@CentOS5 shell]# cat a.txt
- hello
- world
- oh
- my
- god
- [root@CentOS5 shell]# cat a.txt | xargs touch
- [root@CentOS5 shell]# ls
- a.txt god hello my oh world
- [root@CentOS5 shell]# cat a.txt | xargs rm -f
- [root@CentOS5 shell]# ls
- a.txt
xargs 中有个-I选项 可以通过 -I {} 来代表输入参数,如下:
- [root@CentOS5 shell]# cat a.txt
- hello
- world
- oh
- my
- god
- [root@CentOS5 shell]# ls
- a.txt
- [root@CentOS5 shell]# cat a.txt | xargs -I {} touch {}.log
- [root@CentOS5 shell]# ls
- a.txt god.log hello.log my.log oh.log world.log