Linux command play turn Xargs

Source: Internet
Author: User
Tags stdin

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.