Linux remove blank lines from file

 

To remove all blank lines from a file we can use sed command.

There are 2 ways to delete blank lines with sed

  • Removing blank lines within the file.
  • Redirecting std out to new file by removing blank lines from input file.

Example to removing blank lines within the file:

$ sed -i  '/^$/d' INPUT_FILE

Example to redirect std out by removing blank lines from input file:

$ sed '/^$/d' INPUT_FILE > NEW_FILE

or

$ cat INPUT_FILE | sed '/^$/d' > NEW_FILE

-Sany

Advertisements

2 comments on “Linux remove blank lines from file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s