zoukankan      html  css  js  c++  java
  • Shell xargs

    无开关参数:多行并一行

      cat example.txt # Example file
      1 2 3 4 5 6
      7 8 9 10
      11 12
      cat example.txt | xargs
      1 2 3 4 5 6 7 8 9 10 11 12

    -n num:一行变多行,num是每行的个数
      cat example.txt | xargs -n 3
      1 2 3
      4 5 6
      7 8 9
      10 11 12


    -d:使用自定义界定符(delimiter)分割
      echo "splitXsplitXsplitXsplit" | xargs -d X
      split split split split

    传递参数: INPUT | xargs –n X ,X是参数个数  

       args.txt文件:
        arg1
        arg2
        arg3
      cecho.sh文件:
        #!/bin/bash
        #Filename: cecho.sh
        echo $*'#'
      cat args.txt | xargs -n 1 ./cecho.sh #cecho.sh会被调用3次
      输出结果:
        arg1 #
        arg2 #
        arg3 #

    与find一起使用时的问题:   find . -type f -name "*.txt"  -print | xargs rm -f  #xargs默认分隔符是" ",如果文件名是file name.txt时,xargs会出现分割错误,导致删除错误文件

    必须使用find -print0参数使分隔符为\0,并且在xargs中使用-0参数指定分隔符为\0   find . -type f -name "*.txt" -print0 | xargs -0 rm -f

    单独调用变量方法:  

    cat files.txt  | ( while read arg; do cat $arg; done )

  • 相关阅读:
    ASP.NET中JSON的序列化和反序列化
    C# 本地时间和GMT(UTC)时间的转换
    C# XmlReader/XmlWriter 类
    Xml 序列化
    XPath <第四篇>
    XML Schema <第三篇>
    .Net XML操作 <第二篇>
    XML基础<第一篇>
    Sql Server 面试题
    运用计划缓冲的建议
  • 原文地址:https://www.cnblogs.com/FlyCat/p/2851889.html
Copyright © 2011-2022 走看看