Shell xargs (轉)

來源:互聯網
上載者:User

標籤:

  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 )

Shell xargs (轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.