xargs命令是把接收到的数据重新格式化,再将其作为参数提供给其他命令,下面介绍xargs命令的各种使用技巧

一、将多行输入转换成单行输入:

[root@host1 test]# echo -e "1 2 3 4 5 \n6 7 8 \n9 10 11 12" >example.txt
[root@host1 test]# cat example.txt
1 2 3 4 5
6 7 8
9 10 11 12
[root@host1 test]# cat example.txt |xargs
1 2 3 4 5 6 7 8 9 10 11 12

  将单行输入转换成多行输出:

[root@host1 test]# cat example.txt | xargs -n 3
1 2 3
4 5 6
7 8 9
10 11 12

  自定义定界符进行转换(默认的定界符是空格):

[root@host1 test]# echo "Hello:Hello:Hello:Hello" | xargs -d : -n 2
Hello Hello
Hello Hello

二、在脚本中运用:

[root@host1 test]# cat echo.sh
#!/bin/bash
echo $* '^-^'
当参数传递给echo.sh后,它会将这些参数打印出来,并且以"^-^"作为结尾:
[root@host1 test]# echo -e "Tom\nHarry\nJerry\nLucy" > args.txt
[root@host1 test]# cat args.txt | xargs bash echo.sh
Tom Harry Jerry Lucy ^-^
[root@host1 test]# cat args.txt | xargs -n 2 bash echo.sh
Tom Harry ^-^
Jerry Lucy ^-^

  在上面的例子中,我们把参数源都放入args.txt文件,但是除了这些参数,我们还需要一些固定不变的参数,比如:

[root@host1 test]# bash echo.sh  Welcome Tom
Welcome Tom ^-^

  在上述命令执行过程中,Tom是变量,其余部分为常量,我们可以从"args.txt"中提取参数,并按照下面的方式提供给命令:

[root@host1 test]# bash echo.sh  Welcome Tom
[root@host1 test]# bash echo.sh Welcome Herry
[root@host1 test]# bash echo.sh Welcome Jerry
[root@host1 test]# bash echo.sh Welcome Lucy

  这时我们需要使用xargs中-I命令:

[root@host1 test]# cat args.txt  | xargs -I {} bash echo.sh  Welcome {}
Welcome Tom ^-^
Welcome Harry ^-^
Welcome Jerry ^-^
Welcome Lucy ^-^

  -I {} 指定替换字符串,对于每一个命令参数,字符串{}都会被从stdin读取到的参数替换掉,

  使用-I的时候,命令以循环的方式执行,如果有4个参数,那么命令就会连同{}一起被执行4次,在每一次执行中{}都会被替换为相应的参数。

三、结合find使用

  xargs和find是一对非常好的组合,但是,我们通常是以一种错误的方式运用它们的,比如:

[root@host1 test]# find . -type f -name "*.txt" -print | xargs rm -f

  这样做是有危险的,有时会删除不必删除的文件,如果文件名里包含有空格符(' '),则xargs很可能认为它们是定界符(例如,file text.txt会被xargs误认为file和text.txt)。

  如果我们想把find的输出作为xargs的输入,就必须将-print0与find结合使用以字符null('\0')来分隔输出,用find找出所有.txt的文件,然后用xargs将这些文件删除:

[root@host1 test]# find . -type f -name "*.txt" -print0 | xargs -0 rm -f  

  这样就可以删除所有的.txt文件了,xargs -0 将\0作为输入定界符;或者MV掉

[root@host1 test]# find . -type f -name "*.txt" -print0 | xargs -0 -i mv {} /tmp

四、运用while语句和子shell

[root@host1 test]# cat files.txt | (while read arg ;do cat $arg;done)
这条命令等同于:
[root@host1 test]# cat files.txt | xargs -I {} cat {}

  在while循环中,可以将cat $arg替换成任意数量的命令,这样我们就可以对同一个参数执行多条命令,也可以不借助管道,将输出传递给其他命令,这个技巧适应于多种问题场景。子shell操作符内部的多个命令可作为一个整体来运行。

  

xargs命令详解的更多相关文章

  1. [转帖]xargs命令详解,xargs与管道的区别

    xargs命令详解,xargs与管道的区别 https://www.cnblogs.com/wangqiguo/p/6464234.html 之前一直说要学习一下 xargs 到现在为止也没学习.. ...

  2. 【转帖】linux sort,uniq,cut,wc,tr,xargs命令详解

    linux sort,uniq,cut,wc,tr,xargs命令详解 http://embeddedlinux.org.cn/emb-linux/entry-level/201607/21-5550 ...

  3. xargs命令详解,xargs与管道的区别

    为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...

  4. 【转】xargs命令详解,xargs与管道的区别

    为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...

  5. [转]xargs命令详解,xargs与管道的区别

    为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人写的脚本里面也经常会遇到,但是却很容易与管道搞混淆,本篇会详细讲解到底什么是xargs命令,为什么要用xargs命令以 ...

  6. Linux命令:xargs命令详解,xargs与管道的区别

    阅读目录 为什么要用xargs,问题的来源 xargs是什么,与管道有什么不同 xargs的一些有用的选项 回到顶部 为什么要用xargs,问题的来源 在工作中经常会接触到xargs命令,特别是在别人 ...

  7. linux xargs 命令详解

    xargs是给命令传递参数的一个过滤器,也是组合多个命令的一个工具.它把一个数据流分割为一些足够小的块,以方便过滤器和命令进行处理.通常情况下,xargs从管道或者stdin中读取数据,但是它也能够从 ...

  8. Linux xargs命令详解

    find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部 xargs要处理的文件如果不是在结尾,需要加上 -i这个参数 xargs常见命令参数 args:xarg ...

  9. linux每天一小步---xargs命令详解

    1 命令功能 xargs用来从标准输入中执行命令行 xargs命令用来将一些不支持管道传递参数的命令而使之支持 2 命令语法 xargs  [选项参数] commands 3 命令参数 -O 当标准输 ...

随机推荐

  1. Hadoop/Spark开发环境配置

    修改hostname bogon 为localhost 查看ip地址 [training@bogon ~]$ sudo hostname localhost [training@bogon ~]$ h ...

  2. POPTEST老李谈Debug和Release的区别(c#)2

    二.哪些情况下 Release 版会出错 有了上面的介绍,我们再来逐个对照这些选项看看 Release 版错误是怎样产生的 1. Runtime Library: 2. 优化:这类错误主要有以下几种: ...

  3. 利用<meta http-equiv="refresh" content="0;URL=?id='.$id.'" />一条一条的更新数据

    <meta http-equiv="refresh" content="0;URL=?id='.$id.'" /> 解释:页面定时刷新,后面加url ...

  4. vuex 使用文档

    安装 直接下载CDN 引用 <script src="/path/to/vue.js"></script> <script src="/pa ...

  5. TypeScript入门-函数

    ▓▓▓▓▓▓ 大致介绍 TypeScript为JavaScript函数添加了额外的功能,让我们可以更容易地使用.TypeScript中的函数也包括JavaScript中最常见的两种函数 functio ...

  6. JavaScript引用是如何工作的

    原文链接:https://www.sitepoint.com/how-javascript-references-work/ 摘要:JavaScript中没有指针,并且JavaScript中的引用与我 ...

  7. 程序员带你一步步分析AI如何玩Flappy Bird

    以下内容来源于一次部门内部的分享,主要针对AI初学者,介绍包括CNN.Deep Q Network以及TensorFlow平台等内容.由于笔者并非深度学习算法研究者,因此以下更多从应用的角度对整个系统 ...

  8. JQgrid表格的使用

    html部分: <div class="tab"> <table id="datatable"></table>      ...

  9. How to trace the Geolocation of network traffic

    A case about suspicious malware App. A forensic examiner capatured some pcap files and he'd to know ...

  10. jquery转盘抽奖的研究

    先看效果: <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF ...