Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use the sed command to replace multiple empty line with one empty line?

Tags:

sed

I know there is a similar question in SO How can I replace mutliple empty lines with a single empty line in bash?. But my question is can this be implemented by just using the sed command?

Thanks

like image 719
Just a learner Avatar asked Dec 23 '10 17:12

Just a learner


People also ask

How do you add a blank line using sed?

To answer the question as asked, you'd have to do sed 's/pattern. */&\n/' , otherwise you'll insert the newline right after the match instead of at the end of the line.

How do you use sed multiple times?

You can tell sed to carry out multiple operations by just repeating -e (or -f if your script is in a file). sed -i -e 's/a/b/g' -e 's/b/d/g' file makes both changes in the single file named file , in-place.


1 Answers

Give this a try:

sed '/^$/N;/^\n$/D' inputfile 
like image 61
Dennis Williamson Avatar answered Oct 24 '22 23:10

Dennis Williamson