-1

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
  • 95
  • 2
  • 12

4 Answers4

8
sed '/^[[:space:]]*$/d' file >file.modified

This would delete all lines of file that are entirely empty or only contain space-like characters. Space-like characters would in this context include carriage-return characters found at the end of each line in DOS text files (when processed by Unix tools), tabs, and spaces. The resulting data would be written to file.modified.

Removing the redirection and instead using the -i option would do an "in-place" modification of file instead of writing to a new file, but be aware that this option is non-portable and may work differently on different Unix systems (How can I achieve portability with sed -i (in-place editing)?).

Kusalananda
  • 333,661
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
  • 966
  • 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.