Delete empty lines of files in Linux

Source: Internet
Author: User
Document directory
  • 1: Use the SED command
  • 2: run the vim command.
  • 3: shell script

I found many methods on the Internet. Here I just summarized them and used my test results.

1: Use the SED command

DFS is the file name with empty rows

A: sed-I '/^ \ s * $/d' DFS

Note: The-I option is to directly modify the file without printing out the modified content of the file. Without-I, the modified content is printed, but the content of the file is not changed.

B: sed '/^ $/d' DFS

Note: ^ indicates that the beginning $ indicates that the end d is deleted. This sentence indicates that the beginning and end of the line do not contain any characters. Delete

2: run the vim command.

I saw some netizens say that g/^ \ s * $/g can be implemented. I tried and found that it could not work. The following error cannot be recursive:

: G/^ \ s * $/g
E147: Global cannot be recursively executed
Press ENTER or another command to continue

However, you can use g/^ \ s * $/to print the location of the empty row.

: G/^ \ s * $/
4
6
8
11
13
Press ENTER or another command to continue

For more comments on VIM commands, I know they can be implemented.

I got a method by referring to comments from netizens. I know SED is used to delete it with D. I didn't expect it to be done with vim. Sorry... thanks to the two comments!

Delete empty lines under Vim. Command

: G/^ $/D or: G/^ \ s * $/d

3: shell script

If [$ #-ge 1]; then
For each in $ @
Do
If [-F $ each]; then
Name = "tmp _ $ each"
Sed-e '/^ $/d' $ each> $ name
Rm $ each
MV $ name $ each
Else
Echo "rmnone: no such file $ each"
Fi
Done
Else
Echo "rmnone: no input file"
Exit
Fi

This is the script of a netizen. I didn't test it. For convenience, we can give the shell program executable right and put it under/bin, so that we can use this tool like using shell commands.

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.