First, the method of removing the empty line
Sometimes when we process and view files, there are often many empty lines, in order to beauty or need, it is necessary to remove these lines, the method is as follows:
1) using the TR command
Copy the Code code as follows:
Cat filename |tr-s ' \ n '
2) Use SED command
Copy the Code code as follows:
Cat filename |sed '/^$/d '
3) with awk command
Copy the Code code as follows:
Cat filename |awk ' {if ($0!= "") Print} '
Cat filename |awk ' {if (length!=0) print $} '
4) Use the grep command
Copy the Code code as follows:
Grep-v "^$" file name
Second, the method of removing the space
Here's how SED is implemented, and of course awk can.
1. Delete the beginning of the line space
Copy the Code code as follows:
Sed ' s/^[\t]*//g '
Description
The first/left side is s for substitution, and space is replaced with empty.
The right side of the first/is the one that indicates the beginning of the following XX.
The brackets denote "or", a space, or any of the tabs. This is the norm for regular expressions.
The right side of the brackets is *, which indicates one or more.
The second and third \ Have nothing in the middle, which means empty
G means to replace the original buffer (buffer), sed when processing the string is not directly processing the source file, first create a buffer, but plus g to replace the original buffer
The whole means: replace one or more body strings that begin with a space or tab with a null character
2, delete the end of the line space
Copy the Code code as follows:
Sed ' s/[\t]*$//g '
Slightly different from the above is the previous deletion of the ^ symbol, followed by a dollar symbol, which represents the end of the XX string as an object.
Note, however, that in Ksh, the tab is not \ t, but it is simply a tab.
3, delete all the spaces
Copy the Code code as follows:
Sed s/[[:space:]]//g
This article is from the "10732668" blog, please be sure to keep this source http://10742668.blog.51cto.com/10732668/1944831
Shell methods for removing spaces and blank lines