In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to use the xargs command in linux. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
First, convert multi-line input to single-line input:
[root@host1 test] # echo-e "12 3 4 5\ n6 7 8\ n 9 10 11 12" > example.txt [root@host1 test] # cat example.txt 12 3 4 5 6 7 8 9 10 11 12 [root@host1 test] # cat example.txt | xargs 12 3 4 5 6 7 8 9 10 11 12
Convert single-line input to multiple-line output:
[root@host1 test] # cat example.txt | xargs-n 31 2 34 5 67 8 910 11 12
Custom delimiters are converted (the default delimiters are spaces):
[root@host1 test] # echo "Hello:Hello:Hello:Hello" | xargs-d:-n 2Hello HelloHello Hello
Second, use it in the script:
[root@host1 test] # cat echo.sh #! / bin/bashecho $*'^-^'
When the parameters are passed to echo.sh, it prints them out and ends with "^-^":
[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 ^-^
In the above example, we put all the parameter sources in the args.txt file, but in addition to these parameters, we also need some fixed parameters, such as:
[root@host1 test] # bash echo.sh Welcome Tom Welcome Tom ^-^
During the execution of the above command, Tom is a variable and the rest is constant. We can extract parameters from "args.txt" and provide them to the command as follows:
[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
At this point, we need to use the-I command in xargs:
[root@host1 test] # cat args.txt | xargs-I {} bash echo.sh Welcome {} Welcome Tom ^-^ Welcome Harry ^-^ Welcome Jerry ^-^ Welcome Lucy ^-^
-I {} specifies the replacement string, and for each command parameter, the string {} is replaced by the parameter read from stdin
When using-I, the command is executed in a loop, and if there are four parameters, the command is executed four times along with {}, and {} is replaced with the corresponding parameter in each execution.
Third, use in combination with find
Xargs and find are a very good combination, but we usually use them in the wrong way, such as:
[root@host1 test] # find. -type f-name "* .txt"-print | xargs rm-f
It is dangerous to do so, sometimes deleting files that do not need to be deleted, and if the file name contains a space character (''), xargs will probably think of them as delimiters (for example, file text.txt will be mistaken for file and text.txt by xargs).
If we want to use the output of find as the input of xargs, we must use-print0 with find to separate the output with the character null ('\ 0'), use find to find all .txt files, and then delete them with xargs:
[root@host1 test] # find. -type f-name "* .txt"-print0 | xargs-0 rm-f
This allows you to delete all .txt files, and xargs-0 uses\ 0 as the input delimiter.
Fourth, use the whilestatement and sub-shell
[root@host1 test] # cat files.txt | (while read arg;do cat $arg;done)
This command is equivalent to:
[root@host1 test] # cat files.txt | xargs-I {} cat {} after reading the above, do you have any further understanding of how to use the xargs command in linux? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.