Shell deletes empty or blank lines in the file.

Source: Internet
Author: User

Today, we found that there are too many empty lines in a file and we intend to delete them.

So I used SED and checked the command to write it like this:

Sed '/^ $/d' file1

I found that there are still a lot of blank lines in the output content, which is inexplicable.

So: sed '/^ $/P' file1 found that there are few empty rows output, but actually there are many empty rows.

So Cat-A file1 is used.

It was found that many rows contain space characters, which causes regular expressions to not recognize empty rows.

So modify it to: sed '/^ * $/d' file1. Then OK.

In addition, note that the line break in Windows is ^ m $

In Linux, $ is used. If it is a file in Windows, it needs to be converted. It seems like this tool is used to use doc2unix.

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.