Shell methods for removing spaces and blank lines

Source: Internet
Author: User

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

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.