linux命令之玩轉xargs

來源:互聯網
上載者:User

linux命令之玩轉xargs 我們可以用管道將一個命令的標準輸出重新導向到另一個命令的標準輸出,但是有些命令
只能以命令列參數的形式接收資料,而無法接受通過stdin的資料,這個時候我們就要用到xargs。xargs應該緊跟管道操作符之後。 1.將多行轉換為單行,將單行轉換為多行。[root@localhost 案頭]# cat show.txt wangHunan Chenzhou123456 liGuangzhou Zhuhai654321 [root@localhost 案頭]# cat show.txt | xargswang Hunan Chenzhou 123456 li Guangzhou Zhuhai 654321[root@localhost 案頭]# cat show.txt | xargs -n 4wang Hunan Chenzhou 123456li Guangzhou Zhuhai 654321 2.定界符,使用-d 選項[root@localhost 案頭]# cat show.txt | xargs -d "\n"wang Hunan Chenzhou 123456  li Guangzhou Zhuhai 654321  3.讀取stdin,將格式化參數傳遞給命令(一個或多個)[root@localhost 案頭]# vi ceshi.sh#!/bin/bashecho $*'OK'[root@localhost 案頭]# chmod +x ceshi.sh [root@localhost 案頭]# cat show.txt | xargs -n 1 ./ceshi.sh wangOKHunanOKChenzhouOK123456OKliOKGuangzhouOKZhuhaiOK654321OK[root@localhost 案頭]# cat show.txt | xargs ./ceshi.sh wang Hunan Chenzhou 123456 li Guangzhou Zhuhai 654321OK 4.傳遞複雜的命令參數(可變,不可變參數)-I選項替換字元[root@localhost 案頭]# cat show.txt | xargs -I {} ./ceshi.sh -p {} -1-p wang -1OK-p Hunan Chenzhou -1OK-p 123456 -1OK-p li -1OK-p Guangzhou Zhuhai -1OK-p 654321 -1OK 5.xargs結合其他命令的用法[root@localhost 案頭]# file * | grep show | cut -d ":" -f1 | xargs wc -l8 show.txt[root@localhost 案頭]$ file * | grep show | cut -d ":" -f1 |xargs -t wc -lwc -l show.txt 8 show.txt-t選項,先列印命令,然後再執行[root@localhost 案頭]# file * | grep show | cut -d ":" -f1 | xargs vi    開啟vi編輯器[root@localhost 案頭]# file * | grep show | cut -d ":" -f1 | xargs -t -i mv {} {}.bakmv show.txt show.txt.bak 修改名字[root@localhost 案頭]$ find . -name "*.txt" -type f -print0 | xargs -0 tar -zcvf text.tar.gz尋找所有txt檔案,並壓縮它們。其實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.