How to remove blank lines

Hi,
I am facing a problem related to removing blank lines from a text document.

 Input
Error    17-05-2011 11:01:15    VisualSVN Server 2.1   1001    
 
The following information was included with the event:  
 
line3

line4 
  
Error    17-05-2011 11:00:25    VisualSVN Server 2.1    1001    
 
The following information was included with the event:  
 
line3

line4 

Expected Output

Error    17-05-2011 11:01:15    VisualSVN Server 2.1    1001     
The following information was included with the event:  
line3line4 
  
Error    17-05-2011 11:00:25    VisualSVN Server 2.1    1001     
The following information was included with the event:  
line3line4

I tried using

 sed '/^$/d' filename

but to no avail.

Regards,
Mayur

 
grep "." filename
cat temp.txt | sed   /'^$'/d
cat temp.txt | grep -v  '^$'

This is Useless Use of Cat.

Hi,
Thanks for your quick replies. But the solution you mentioned is not working also the file size is huge but i don't think it will have greater impact. The solution gives me the output as below

The following information was included with the event: 

line3
line4
 
Error   17-05-2011 11:00:25     VisualSVN Server 2.1    1001

The following information was included with the event: 

line3
line4

But my desired output is a little different from given above.

It might be that "empty" lines are not completely empty but filled with spaces (or other non-printable characters). Try the following:

sed '/^[<blank><tab>]*$/d' /path/to/input > /path/to/output

Replace "<blank>" and "<tab>" with literal blank/tabulator characters.

Still, the way you presented your expected output was a bit different: you wanted to retain a blank line before every paragraph starting with "Error". To accomplish this try:

sed '/^[<blank><tab>]*$/d
     /^Error.*$/ s/^/^M/' /path/to/input > /path/to/output

The "^M" is a literal newline. You get it (in vi) by switching to insert-mode, pressing CRTL-V and then ENTER.

I hope this helps.

bakunin

As bakunin mentioned, it seems there are many lines that seem to be empty but consist of spaces.

$> cat ./mach.ksh
awk '
        /^Error/ {print l?l RS RS $0: RS $0; l=x; next}
        /^line/ {l=sprintf("%s%s", l, $0); next}
        /^[[:space:]]*$/ {next}
        {print}
        END{
                if(l){print l}
        }
' infile
$> ./mach.ksh

Error    17-05-2011 11:01:15    VisualSVN Server 2.1   1001
The following information was included with the event:
line3line4

Error    17-05-2011 11:00:25    VisualSVN Server 2.1    1001
The following information was included with the event:
line3line4

Hi,
Thanks for your replies. I will definitely try the solutions. It never came to my mind the file is a text file converted from ".evtx" format. So it definitely has some junk in it.

Regards,
Mayur

---------- Post updated 05-26-11 at 11:31 AM ---------- Previous update was 05-25-11 at 07:38 PM ----------

Hi,
Thanks a lot bakunin and zaxxon. I understood bakunin's solution but was not able to comprehend zaxxon's solution will take some awk reading and get through it. Meanwhile, i came up with this temporary solution

sed '/^[[:space:]]*$/d' filename 

Regards,
Mayur

Hi,

try this out --

cat filename|sed "/^[ ]*$/d" ; 

it should work.

thankx,
Arpit

sed '/^$/d'

The above command delete the blank line

hello anil,

i think same is required here.

@mayursingru

/^Error/ {print l?l RS RS $0: RS $0; l=x; next} --> If the line starts with the string "Error" then print l, but when it has a value, use l and two times the record separator (RS) which is a newline, and additionally the current line ($0). If l has no value at the moment, kust print one record separator and the current line. This will make sure, that you have an empty line as separator for the next block starting with "Error". Afterwards we set the value of l to nothing (l=x) and skip the rest of the awk script for this input line (next).

/^line/ {l=sprintf("%s%s", l, $0); next} --> If a line starts with "line", we store into the variable l the current line and if there have been former lines starting with "line", they will be already stored in l and just being appended. With this, you will not have every "line..." on its own row but concatenated them all into one, as your example output shows. We then skip the rest of the awk script for that line again (next).

/^[[:space:]]*$/ {next} --> If a line is "empty", but we recognized that in your example there are spaces in it which can be blanks or tabs or mixed at any number, we just skip them. [[:space:]] is a POSIX bracket expansions (see this Regex Tutorial - POSIX Bracket Expressions). The rest of the awk script is skipped again.

{print} --> If none of the before 3 conditions matched, ie. a line does not start with Error, line or contains spaces (or none of them), we just print the rest of the lines, which in your example is the one starting with "The following ...".

END{ if(l){print l} } When the whole file has been worked through, we just check, if l still has a value in it (this should be a line starting with "line"), we have to print it now, since we formerly had a line starting with "Error" triggering this print which will not happen anymore.

Hi
can you please try this

grep -v "^$" input_file > out_file

Hi zaxxon,
Thanks a ton.

Regards,
Mayur