Blank lines problems 
Author Message
 Blank lines problems

Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
"\n" , and I want to remove one of them. "The AWK programming language"
doesn't give me anything.

Thanks



Fri, 19 Jan 2001 03:00:00 GMT  
 Blank lines problems

Quote:

> Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
> "\n" , and I want to remove one of them. "The AWK programming language"
> doesn't give me anything.


a a a a a
b b

ccc
d d d

a a a a a
b b
ccc
d d d

--
Maurizio Loreti                      http://wwwcdf.pd.infn.it/~loreti/mlo.html



Fri, 19 Jan 2001 03:00:00 GMT  
 Blank lines problems


Quote:
>Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
>"\n" , and I want to remove one of them. "The AWK programming language"
>doesn't give me anything.

Your request is not very clear

a. If you are trying to remove totally blank line (ie no spaces, tabs wotever..)
    if ($0 == "" ) next

b. If it is only duplicate blanks, then try this
   {
    if (pc == "" && $0 == "") next
    pc=$0; print}

c. If you are trying to remove multiple blanks (and multiple anything) then
   you are better off using 'uniq' ( man uniq under UNIX)

Mark
--
Mark Katz
ISPC, London - Innovation in data-delivery tools
Tel: (44) 181-455 4665, Fax (44) 181-458 9554
** See our website at http://www.efiche.com/efiche **



Fri, 19 Jan 2001 03:00:00 GMT  
 Blank lines problems

Quote:



>>Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
>>"\n" , and I want to remove one of them. "The AWK programming language"
>>doesn't give me anything.

>Your request is not very clear

I think it is pretty clear - he wants to do the 'uniq' thing - that is,
squish multiple blank lines into one.
...

Quote:
>c. If you are trying to remove multiple blanks (and multiple anything) then
>   you are better off using 'uniq' ( man uniq under UNIX)

Although uniq is {*filter*}, he did ask for an AWK solution, and it is often the case
that it needs to be part of a larger whole, that is already written in AWK.

In any case, the shell script below fits a slightly different spec, but I
find it quite useful for "taming" random text files I download from the net.
In particular, it works well as a "preprocessor" to something like fmt or par.
Hope it helps...

#!/bin/sh
# Shell version (converted from original AWK script): 1/28/96
tmp=/tmp/tmp$$
trap "rm -f $tmp" 0 1 2 3 15
set -e
for i
    do
        gawk '
# Delete single blank lines - reduce multiple blank lines to a single line.
!NF     {
        for (i=0; getline && !NF; i++);
        if (i) print ""
        }
{ print }' $i > $tmp
    mv $tmp $i
    done

************************************************************************
It is not enough that others fail.  I must succeed!


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer



Fri, 19 Jan 2001 03:00:00 GMT  
 Blank lines problems

Quote:


>>Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
>>"\n" , and I want to remove one of them. "The AWK programming language"
>>doesn't give me anything.

>awk 'NF {print}'

Bzzzt!  First of all, that should just be: awk NF
(which is 6 bytes to your 16 (heh heh))

Second, what the original posters actually wants is:

        1
        !NF { while(getline t && !length(t)); print }

************************************************************************
Pure balls. (David Spade)


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer



Fri, 19 Jan 2001 03:00:00 GMT  
 Blank lines problems


:>Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
:>"\n" , and I want to remove one of them. "The AWK programming language"
:>doesn't give me anything.

: Your request is not very clear

: a. If you are trying to remove totally blank line (ie no spaces, tabs wotever..)
:     if ($0 == "" ) next

awk 'BEGIN{RS="\n+"}//'

May also be usefull.

--
See http://www.mauve.demon.co.uk/    |Linux PDA, cheap electronics/PC bits sale.
See_header,_for_UCE_policy___________|_____________________________Ian_Stirling.
Paranoia: A game for the whole family, and anyone else who might be watching.



Fri, 19 Jan 2001 03:00:00 GMT  
 Blank lines problems

:>Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
:>"\n" , and I want to remove one of them. "The AWK programming language"
:>doesn't give me anything.

: awk 'NF {print}'

And thence, to the absolutely shortest possible solution:
awk NF

--
See http://www.mauve.demon.co.uk/    |Linux PDA, cheap electronics/PC bits sale.
See_header,_for_UCE_policy___________|_____________________________Ian_Stirling.
Paranoia: A game for the whole family, and anyone else who might be watching.



Fri, 19 Jan 2001 03:00:00 GMT  
 Blank lines problems
Since you may get fed "blank" lines that are not just an eol character, but
spaces and tabs, I've found that the following is pretty robust :

/^[ \t]*$/ { next }



Quote:
> Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
> "\n" , and I want to remove one of them. "The AWK programming language"
> doesn't give me anything.

> Thanks



Sat, 20 Jan 2001 03:00:00 GMT  
 Blank lines problems

Quote:




>>>>Can anyone tell me how to remove blank lines using AWK. I.e.: There are 2
>>>>"\n" , and I want to remove one of them. "The AWK programming language"
>>>>doesn't give me anything.
...
>>Second, what the original posters actually wants is:

>>        1
>>        !NF { while(getline t && !length(t)); print }

>That is not what he said (note that awk NF changes "a\n\nb" to "a\nb"),
>but now that I look at it, your answer makes more sense.

Oops!!!  Now, my face is red.

Despite having followed this thread for a while, I just now noticed that
he said "There are 2 "\n"", not "There are 2 blank lines"

Oh well.  Reading is a skill...

************************************************************************
Sex without love is an empty experience, but, as empty experiences go,
it's one of the best.
                -- Woody Allen


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer



Sat, 20 Jan 2001 03:00:00 GMT  
 Blank lines problems


Quote:
>Since you may get fed "blank" lines that are not just an eol character, but
>spaces and tabs, I've found that the following is pretty robust :

>/^[ \t]*$/ { next }

That's the reason all the solutions posted so far use NF - because AWK
magically "deletes" the spaces and tabs for you in calculating the field
count.  So, your method using the reg exp is eqv to just: !NF { next }

Of course, one could just as easily argue the opposite - that lines contains
spaces and/or tabs are *not* blank lines.  So, you make the call!

************************************************************************
Drink Coke...  (Bill Hicks - 12/16/61-2/25/94)


          hundreds, if not thousands, of dollars, every time he posts -
************************************************************************
rwvpf wpnrrj ibf ijrfer



Sat, 20 Jan 2001 03:00:00 GMT  
 Blank lines problems
Good point. A more elegant solution. That will save me some typing in the
future. Thanks.



Quote:


> >Since you may get fed "blank" lines that are not just an eol character,
but
> >spaces and tabs, I've found that the following is pretty robust :

> >/^[ \t]*$/ { next }

> That's the reason all the solutions posted so far use NF - because AWK
> magically "deletes" the spaces and tabs for you in calculating the field
> count.  So, your method using the reg exp is eqv to just: !NF { next }

> Of course, one could just as easily argue the opposite - that lines
contains
> spaces and/or tabs are *not* blank lines.  So, you make the call!

> ************************************************************************
> Drink Coke...  (Bill Hicks - 12/16/61-2/25/94)


>      hundreds, if not thousands, of dollars, every time he posts -
> ************************************************************************
> rwvpf wpnrrj ibf ijrfer



Mon, 22 Jan 2001 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. remove blank lines before lines beginning with string

2. blank lines/lines with spaces (how to remove)

3. inserting a blank line in a file

4. to insert a blank line

5. adding blank lines to text file

6. print contents up to blank line

7. datafile has blank lines

8. Blank lines

9. Removing data between blank lines

10. line beginning with a blank

11. blank lines

12. how to specify a blank line as record separator

 

 
Powered by phpBB® Forum Software