-2

I have an output in a file with the blank spaces like

daily/A3D05180000052121001 30698

daily/A3D05180000052200001 30698

daily/A3D05180000052203001 30698

I need to remove the empty lines and my output should be like

daily/A3D05180000052121001 30698
daily/A3D05180000052200001 30698
daily/A3D05180000052203001 30698

I tried using sed

sed -i -e '/./,$!d' -e :a -e '/^\n*$/{$d;N;ba' -e '}' "$FILESIZE3"

But it did not work out... Can anyone pls help me out on this

clarie
  • 85
  • 2
  • 11
  • Maybe try `grep -v "^$" $OLDNAME > $NEWNAME`? – 0xC0000022L May 19 '20 at 08:50
  • 1
    see also [Delete empty lines using sed](https://stackoverflow.com/questions/16414410/delete-empty-lines-using-sed) – Sundeep May 19 '20 at 08:58
  • Are the lines _completely_ empty, or may there be spaces and/or tabs on them? – Kusalananda May 19 '20 at 08:59
  • @Kusalananda I had some content and i deleted those contents and saved in the file...So the place where i removed the contents empty spaces came – clarie May 19 '20 at 09:01
  • 3
    @clarie Sorry, but that's ever so slightly ambiguous. Are there _spaces_ on the otherwise empty lines? Also, is the file saved with a Windows editor, i.e. is it a DOS text file? – Kusalananda May 19 '20 at 09:05
  • 1
    @clarie If you don't understand what Kusalananda asked you, you can use `cat -E file` to display the file's content in a way that we can see if empty lines are indeed empty or if they have spaces. – Quasímodo May 19 '20 at 10:40

4 Answers4

6
sed -i '/^[[:space:]]*$/d' file

This would delete all lines of file that only contains space-like characters. This includes carriage-return characters found at the end of each line in DOS text files (when processed by Unix tools), and also tabs and spaces.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
0

Not sure what the rest of your sed does but isn't this what you're looking for?

host:~ # sed -e '/^$/d' file.txt
daily/A3D05180000052121001 30698
daily/A3D05180000052200001 30698
daily/A3D05180000052203001 30698

Please clarify the requirement if this doesn't do the trick.

eblock
  • 956
  • 4
  • 7
  • `sed -i -e '/^$/d' "$FILESIZE3"` I gave like this...it didn't work. I added -i to edit file inplace...can u help me on this – clarie May 19 '20 at 09:00
0

AWK representation :

awk '!/^$/{ print }' infile > outfile && mv outfile infile 
0

In vim, I use the global command to remove empty lines. Not sure if it helps in your case.

#:[range]g/pattern/cmd

The example acts on the specified [range] (default whole file), by executing the command cmd for each line matching pattern (an executing command is one starting with a colon such as :d for delete in vi). In your case it can be

:g/^$/d

If you have spaces or tabs in between

:g/^[\s\t]*$/d

Hope it helps.