TECH SOLUTIONS

Click here to edit subtitle

Forums

Post Reply
Forum Home > Unix Learnings > Remove blank lines in a file without creating another/temporary file

Sourav Gulati
Site Owner
Posts: 83

Here is a test file :

$ cat test

hi


how


are


you

$

 

 

Now, if you want to remove all the blank lines from a file , you can do it as follows:

$ cat test | grep -v  '^$' > test1

$ mv test1 test

But , here we are creating a temporary file "test1"

Now , if you want to remove all the blank lines from a file without creating temporary file, it can be done as follows:

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

"sed -i" overwrites the file itself

so it will remove blank lines and override the file.

Now, let's run the cat command on test

$cat test

hi

how

are

you

 $



 

 

 

 


--


August 20, 2012 at 5:38 AM Flag Quote & Reply

You must login to post.