日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux之xargs

發(fā)布時間:2025/3/20 linux 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux之xargs 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
可以用管道將一個命令的stdout(標準輸出)重定向到另一個命令的stdin(標準輸入)。例如: [root@localhost Test]# cat sh1.3.sh | grep fruit

? ? ? ?但是,有些命令只能以命令行參數(shù)的形式接受數(shù)據(jù),而無法通過stdin接受數(shù)據(jù)流。在這種情況下,我們沒法用管道來提供那些只有通過命令行參數(shù)才能提供的數(shù)據(jù)。那就只能另辟蹊徑了。該xargs命令出場了,它擅長將標準輸入數(shù)據(jù)轉(zhuǎn)換成命令行參數(shù)。xargs能夠處理stdin并將其轉(zhuǎn)換為特定命令的命令行參數(shù)。xargs也可以將單行或多行文本輸入轉(zhuǎn)換成其他格式,例如單行變多行或是多行變單行。

find?/sbin?-perm?+700?|ls?-l???????這個命令是錯誤的

find?/sbin?-perm?+700?|xargs?ls?-l???這樣才是正確的

xargs?可以讀入?stdin?的資料,并且以空白字元或斷行字元作為分辨,將?stdin?的資料分隔成為?arguments?。?因為是以空白字元作為分隔,所以,如果有一些檔名或者是其他意義的名詞內(nèi)含有空白字元的時候,?xargs?可能就會誤判了~他的用法其實也還滿簡單的!就來看一看先!

xargs命令應(yīng)該緊跟在管道操作符之后,以標準輸入作為主要的源數(shù)據(jù)流。它使用stdin并通過提供命令行參數(shù)來執(zhí)行其他命令。

xargs命令把從stdin接收到的數(shù)據(jù)重新格式化,再將其作為參數(shù)提供給其他命令。xargs可以作為一種替代,其作用類似于find命令中的 -exec。下面是各種xargs命令的使用技巧。

1、多行輸入轉(zhuǎn)換成單行輸出


[root@localhost Test]# cat exam.txt #!/bin/bash 1 2 3 4 5 6 7 8 [root@localhost Test]# cat exam.txt | xargs


2、將單行輸入轉(zhuǎn)換成多行輸出


[root@localhost Test]# vim exam.txt [root@localhost Test]# cat exam.txt | xargs -n 3 #!/bin/bash 1 2 3 4 5 6 7 8

現(xiàn)在來看看xargs使用的選項參數(shù)

xargs [-0prtx] [-E eof-str] [-e[eof-str]] [--eof[=eof-str]] [--null] [-d delimiter] [--delimiter delimiter] [-I replace-str] [-i[replace-str]][--replace[=replace-str]] [-l[max-lines]] [-L max-lines] [--max-lines[=max-lines]] [-n max-args] [--max-args=max-args] [-s max-chars][--max-chars=max-chars] [-P max-procs] [--max-procs=max-procs] [--interactive] [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file][--show-limits] [--version] [--help] [command [initial-arguments]]

-a 從文件中讀入而不是標準輸入中讀取

[root@localhost Test]# vim test.txt [root@localhost Test]# cat test.txt aa bb cc dd ee ff gg [root@localhost Test]# xargs -a test.txt aa bb cc dd ee ff gg

-0 當(dāng)輸入有特殊字符時,將其作為一般的字符處理,如有空格

[root@localhost Test]# echo "//" | xargs // [root@localhost Test]# echo "//" | xargs -0

-d 指定分隔符

[root@localhost Test]# cat test.txt aa bb cc dd ee ff gg [root@localhost Test]# cat test.txt | xargs -d "c"

-E eof-str ,指定結(jié)束標志為eof-str,xargs處理到這個標志就會停止

[root@localhost Test]# cat test.txt aa bb cc dd ee ff gg [root@localhost Test]# xargs -E "dd" -a test.txt aa bb cc [root@localhost Test]# cat test.txt | xargs -E "dd" aa bb cc

-L?max-lines: 每次讀取max-line行輸入交由xargs處理

[root@localhost Test]# cat test.txt aa bb cc dd ee ff gg [root@localhost Test]# cat test.txt | xargs -L 2 aa bb cc dd ee ff gg

-l: 類似于-L,區(qū)別在于-l可以不指定參數(shù),默認為1.

-n?max-args: 每行執(zhí)行max-args個輸入,默認執(zhí)行所有

[root@localhost Test]# cat test.txt | xargs -n 2 aa bb cc dd ee ff gg

-t: 先打印執(zhí)行的命令,然后執(zhí)行

[root@localhost Test]# cat test.txt | xargs -t /bin/echo aa bb cc dd ee ff gg aa bb cc dd ee ff gg -I? replace-str : 將每行輸入內(nèi)容替換為 replace-str
[root@localhost Test]# cat test.txt | xargs -t -I {} echo {} >> test.txt echo aa bb cc dd echo ee ff gg

《新程序員》:云原生和全面數(shù)字化實踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Linux之xargs的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。