Linux command play turn Xargs
We can use the pipeline to redirect the standard output of one command to the standard output of another command, but some commands
You can only receive data in the form of command-line arguments, and you cannot accept data through stdin, which is when we need to use Xargs.
Xargs should follow the pipeline operator.
1. Convert multiple lines to a single line and convert a single row to multiple rows.
[Email protected] Desktop]# cat Show.txt
Wang
Hunan Chenzhou
123456
Li
Guangzhou Zhuhai
654321
[Email protected] Desktop]# cat Show.txt | Xargs
Wang Hunan Chenzhou 123456 Li Guangzhou Zhuhai 654321
[Email protected] Desktop]# cat Show.txt | Xargs-n 4
Wang Hunan Chenzhou 123456
Li Guangzhou Zhuhai 654321
2. Delimiter, using the-D option
[Email protected] Desktop]# cat Show.txt | Xargs-d "\ n"
Wang Hunan Chenzhou 123456 Li Guangzhou Zhuhai 654321
3. Read the stdin and pass the format parameter to the command (one or more)
[Email protected] Desktop]# VI ceshi.sh
#!/bin/bash
echo $* ' OK '
[Email protected] Desktop]# chmod +x ceshi.sh
[Email protected] Desktop]# cat Show.txt | Xargs-n 1./ceshi.sh
Wangok
Hunanok
Chenzhouok
123456OK
Liok
Guangzhouok
Zhuhaiok
654321OK
[Email protected] Desktop]# cat Show.txt | Xargs./ceshi.sh
Wang Hunan Chenzhou 123456 Li Guangzhou Zhuhai 654321OK
4. Pass complex command parameters (variable, immutable parameter)-I option replace character
[Email protected] Desktop]# 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 use with other commands
[[Email protected] Desktop]# file * | grep Show | Cut-d ":"-F1 | Xargs wc-l
8 Show.txt
[[Email protected] Desktop]$ file * | grep Show | Cut-d ":"-f1 |xargs-t wc-l
Wc-l Show.txt
8 Show.txt
-T option, print the command first, and then execute the
[[Email protected] Desktop]# file * | grep Show | Cut-d ":"-F1 | Xargs VI Open VI Editor
[[Email protected] Desktop]# file * | grep Show | Cut-d ":"-F1 | XARGS-T-i mv {} {}.bak
MV Show.txt Show.txt.bak
Modify Name
[[Email protected] Desktop]$ find. -name "*.txt"-type f-print0 | xargs-0 TAR-ZCVF text.tar.gz
Find all txt files and compress them.
In fact, Xargs can take a lot of orders, you will find them very interesting.
- This article is from: Linux Learning Network
Linux command play turn Xargs