954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Removing Blank lines in a file

I want to remove blank lines (empty or with spaces and tabs) from a file from within a perl script.
here's what i tried.

`sed '/^ *\$/d' $file1`;

Does not seem to work for some reason.
Any Help please?

voidyman
Newbie Poster
17 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
 

Take out the space (unless you are looking for lines with just one space)

^$

So, given a file called test.txt, JUST the sed part would be:

sed '/^$/d' test.txt
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

...or if you just want to do it from the command-line

perl -pi -e "s/^\n//" test.txt
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

thanks for your replies. $^ matches a blank line(but not a line with spaces). I eventually ended up calling the perl inline from my script. :)

voidyman
Newbie Poster
17 posts since Sep 2011
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: