Linux Command (44): Remove empty lines from all files

Source: Internet
Author: User

Method One: Using grep

Grep-v ' ^\s*$ ' test.txt

Note:-V indicates that the matching result is reversed, and the regular expression matches the blank line. (blank lines can include whitespace characters such as character tabs)

Method two: Using SED

Sed '/^\s*$/d ' test.txt

Note: D represents the deletion of the row

Method Three: Using awk

awk NF Test.txt

Note: NF represents the number of fields in the current row, the number of fields in a blank line is 0, and awk is interpreted as false, so no output is made.

There are three ways to handle blank lines that contain whitespace characters (spaces, tabs, and so on).

Method Four: If the blank line is caused by ' \ n ', you can also use the TR command to remove the empty line

Tr-s ' \ n ' < test.txt

Note:-S represents the compression of multiple consecutive characters into a single character, where multiple ' \ n ' are compressed into a ' \ n ', which results in the removal of empty lines.

The level is limited, if has the improper place, also looks correct!

Linux Command (44): Remove empty lines from all files

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.