Shell practical tip: Quickly remove space in a string _linux shell

Source: Internet
Author: User

In the process of sorting out the word library encountered a problem, some are actually the same keyword with a space, can not be normal to be heavy, then there is no way to quickly remove these characters in the middle of the space? After Baidu some, find a solution, record.

The effect shown in the image below is the sample text, and the bottom half is the result of removing the space with the shell. This is the 3rd method below.

A common problem with using the SED command for string processing on UNIX is how to delete a space at the end of a line.

Here's how SED is implemented, and of course awk can.

1, delete the beginning of the space

Copy Code code as follows:

Sed ' s/^[\t]*//g '

Description

The first/left is the substitution of the s, and the space is replaced with null.
The first/right is the beginning of the following XX.
The brackets denote "or", either in a space or in the tab. This is the specification of the regular expression.
The right side of the bracket is *, representing one or more.

There is nothing in the second and third \ Middle, which means null.

G means to replace the original buffer (buffer), sed in the processing of strings does not directly deal with the source file, first create a buffer, but add g to the original buffer to replace

The whole idea is to replace one or more body strings with spaces or tabs with null characters.

2, delete the end of the line space

Copy Code code as follows:

Sed ' s/[\t]*$//g '

Slightly different from the above is the previous deletion of the ^ character, followed by a dollar sign, which means the end of XX string as an object.

However, note that in Ksh, the tab is not \ t but directly into a tab.

3, delete all the blanks

Copy Code code as follows:

Sed s/[[:space:]]//g

Related Article

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.