linux xargs 将标准输入数据转换成命令行参数

1 将多行输入转化为单行输出:
[root@localhost test]# cat text.txt
1
2
3

[root@localhost test]# cat text.txt | xargs
1 2 3
2将单行输入转成多行输出
[root@localhost test]# cat example.txt 
1 2 3 4 5 6 7 8 9 01 2 3 4 5 6 7 8 9 0
[root@localhost test]# cat example.txt | xargs -n 4        -n 每行n个参数 
1 2 3 4
5 6 7 8
9 01 2 3
4 5 6 7
8 9 0
3 -d 以什么分隔  
[root@localhost test]# echo "hello lufubo my name is haha" | xargs -d m
hello lufubo  y na e is haha


[root@localhost test]# echo "hello lufubo my name is haha" | xargs -d m -n 2
      hello lufubo  y na
      e is haha
4 读取stdin,将格式化参数传递给命令
[root@localhost test]# cat test.sh 
#! /bin/sh
echo $*'#'

[root@localhost test]# cat args.txt 
arg1
arg2
arg3

[root@localhost test]# cat args.txt | xargs -n 1 ./test.sh 
arg1#
arg2#
arg3#

[root@localhost test]# cat args.txt | xargs -n 2 ./test.sh  
arg1 arg2#
arg3#

[root@localhost test]# cat args.txt | xargs -I {} ./test.sh -p {} -l                   -I 替换
-p arg1 -l#
-p arg2 -l#
-p arg3 -l#
    
5 find 与 xargs的结合使用
    find . -type f -name "*.tmp" -print0 | xargs -0 rm -f
    删除所有.tmp文件,xargs -0 表示以\0作为输入定界符
    
    find . -type f -name "*.c" -print0 | xargs -0 wc -l
    统计当前目录下所有.c文件的总行数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值