Use Xargs to convert multiple lines of text into a single line under Linux and comma-separated by tr

Source: Internet
Author: User

Get ready:

Cat Test.txt 1 3 4

Example:

Cat Xargs 1 3 4

You can see that the resulting string is separated by a space.

Separate the above string with a comma, you can use the TR command to replace the space

Cat Xargs TR ' ' ' , '

However, the TR command can only replace a single character, and multiple characters cannot be implemented.

Use SED to implement the substitution of multiple strings:

Cat Xargs sed /s//','/g

SED can replace more than one string, can compensate for the shortage of TR, the following G means matching multiple.

Summarize:

There are many substitution and merge commands, mainly to see how you solve the problem, mainly based on Xargs and TR implementation, of course, you can also use the SED command to replace, sed command support multiple strings.

Reference:

Http://man.linuxde.net/xargs (Xargs usage)

HTTP://MAN.LINUXDE.NET/TR (TR usage)

http://man.linuxde.net/sed (SED usage)

Http://bbs.chinaunix.net/thread-1631249-1-1.html

Use Xargs to convert multiple lines of text into a single line under Linux and comma-separated by tr

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.