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:
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:
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: