Can't remove blank lines from a file

Hi Guys,

I have been trying to remove blank lines from a file with no success. I tried using all the following options on the file:

tr -s '\n' < abc.txt

grep -v "^$" abc.txt

sed '/^$/d' abc.txt

sed '/./!d' abc.txt

awk '/./' abc.txt

The file is a text file.

The output file is unchanged. I am running cygwin with bash shell.

Thanks.

You realize of course that none of the commands you've given actually edits the given file. Just checking the obvious here. :wink:

Since this is cygwin another culprit may be the CPM-style carriage returns Windows text files have. Annoyingly, plenty of things can't actually match a carriage return.

tr -d '\r' < input | grep -v "^$" > output

you sure those are blank line and not the spaces and/or tabs?

try:

sed 's/^[ \t]*$//g' file

That will convert spaces into blank lines but not actually delete the blank lines.

Have you tried this one (delete any line starting with a newline):

sed -e "/^$/d" abc.txt

yes..that's correct. what I wanted to tell is the same as your. (try deleting blank lines then after).
I agree it was an incomplete solution.

try awk

awk 'NF' abc.txt

Dear Friend,

You can use the following code, to delete the blank lines from a file

The file contains the following content
welcome1

welcome2

welcome3

 
sed -i -e '/^$/D'  file 

After execute the above code, the file content is
welcome1
welcome2
welcome3

Here -i is used to affect the file

You use the following command to remove the blank lines from a file.

sed -i '/^$/d' filename

d - This is used to clear the content of the pattern space.
D - This is used to remove the first line in the pattern space.Not all the contents.
So,d is preferable than D.

sed -i '/./!d' input file.

This also will remove the blank line from the input file

grep . infile > outfile

My money's on Corona688 and carriage returns in the data. So far, all other suggestions in the thread are variations on attempts listed in the original post. If those failed, so will the suggestions.

Just my 2 cents :wink:

If Corona688's suggestion does not work, take a look at the file using od to determine if those blank lines contain an "invisible" byte other than \r :

od -c inputfile

Cheers,
Alister