Using gawk to replace blank lines 
Author Message
 Using gawk to replace blank lines

Hiyall,

i'm trying to use gawk to replace blank lines with other text.  I've tried
doing comparisons such as:

 if( NF == 0)
   ...

and

 if( length($0) == 0 )
   ...

but neither work.

Basically what i want is to change a file from:

-----------
The next line is blank.

The previous line is blank.
-----------

to
-----------
The next line is blank.
<This used to be a blank line>
The previous line is blank.
-----------

many thanks.

albert.

Supernews - Discussions Start Here
http://www.*-*-*.com/



Mon, 25 Dec 2000 03:00:00 GMT  
 Using gawk to replace blank lines
: Hiyall,

: i'm trying to use gawk to replace blank lines with other text.  I've tried
: doing comparisons such as:

:  if( NF == 0)
:    ...

: and

:  if( length($0) == 0 )
:    ...

: but neither work.

: Basically what i want is to change a file from:

: -----------
: The next line is blank.

: The previous line is blank.
: -----------

: to
: -----------
: The next line is blank.
: <This used to be a blank line>
: The previous line is blank.
: -----------

: many thanks.

: albert.

awk '!NF { print "<This used to be a blank line>";next}1'

Cryptic,  what?

!NF evaluates to true when awk parses the current line and finds
no fields.  That causes the action to be invoked to print
the message.  Next causes awk to cease processing the current
line,  fetch a new line and restart processing.
1 is a pattern that is always true.  The default action is to
print the line.  You could also write:

awk 'NF {print;next}{print "<This used to be a blank line>"}'

or

awk '!NF { $0 = "<This used to be a blank line>"}1'

which is actually the shortest.  In this one,  blank lines are
replaced by the message,  then all lines are printed by the "1" statement.

: Supernews - Discussions Start Here
: http://www.supernews.com

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Mon, 25 Dec 2000 03:00:00 GMT  
 Using gawk to replace blank lines

Quote:

>Hiyall,

>i'm trying to use gawk to replace blank lines with other text.  I've tried
>doing comparisons such as:

> if( NF == 0)
>   ...

>and

> if( length($0) == 0 )
>   ...

>but neither work.

>Basically what i want is to change a file from:

>-----------
>The next line is blank.

>The previous line is blank.
>-----------

>to
>-----------
>The next line is blank.
><This used to be a blank line>
>The previous line is blank.

All these forms will work:

gawk '{print (NF==0)?"<This used to be a blank line>":$0}' in > out

gawk 'NF==0{print "<This used to be a blank line>"}NF!=0{print}' in > out

gawk 'NF==0{print "<This used to be a blank line>";next}{print}' in > out

gawk '{if(NF==0){print "<This used to be a blank line>"}else{print}}' in > out

gawk '/^$/{print "<This used to be a blank line>";next}{print}' in > out

gawk 'length==0{print "<This used to be a blank line>";next}{print}' in > out

There are other variations.  Some of these allow whitespace (spaces and
tabs) on the "blank" lines.

length and length($0) are equivalent.

see man gawk

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 25 Dec 2000 03:00:00 GMT  
 Using gawk to replace blank lines


Quote:

>: Basically what i want is to change a file from:

>: -----------
>: The next line is blank.

>: The previous line is blank.
>: -----------

>: to
>: -----------
>: The next line is blank.
>: <This used to be a blank line>
>: The previous line is blank.
>: -----------

>awk '!NF { print "<This used to be a blank line>";next}1'

>Cryptic,  what?

[snip]

>awk 'NF {print;next}{print "<This used to be a blank line>"}'

>or

>awk '!NF { $0 = "<This used to be a blank line>"}1'

Not as cryptic, and almost as short is this:

awk '{print NF ? $0 : "<This used to be a blank line>"}'

assuming your awk supports the ? : operator.  :-)

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Mon, 25 Dec 2000 03:00:00 GMT  
 Using gawk to replace blank lines

Quote:

> Hiyall,

> i'm trying to use gawk to replace blank lines with other text.  I've tried

> doing comparisons such as:

>  if( NF == 0)

>    ...

> and

>  if( length($0) == 0 )

>    ...

> but neither work.

> Basically what i want is to change a file from:

> -----------

> The next line is blank.

> The previous line is blank.

> -----------

> to

> -----------

> The next line is blank.

> <This used to be a blank line>

> The previous line is blank.

> -----------

> many thanks.

> albert.

> Supernews - Discussions Start Here
> http://www.supernews.com

Looking for a blank line : awk nawk

cat filename | awk '

$0 ~ /^$/ {   dosomething
                print a line
                next }

{ print  $0 }'



Sat, 06 Jan 2001 03:00:00 GMT  
 Using gawk to replace blank lines

Quote:
> cat filename | awk '

> $0 ~ /^$/ {   dosomething
>            print a line
>            next }

> { print  $0 }'

The $0 ~ is totally redundant!

/^$/ { ... is the "correct' syntax.

--
Best regards,
 _ __                      _    ,   _ _ _
' )  )     /         _/_  ' )  /   ' ) ) )
 /--' ____/___/> __  /     /--/     / / / __,_  __  o _   ______
/  \_(_) /_) (__/ (_<__   /  ( o   / ' (_(_) (_/ (_<_/_)_(_) / <_

Robert H. Morrison                      Tel:   +49 721 9628 167
Software Development, Basis Team        FAX:   +49 721 9628 261



Sat, 06 Jan 2001 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Replacing blank lines with text.

2. remove blank lines before lines beginning with string

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

4. How to replace or create a file using the open/create/replace.vi

5. inserting a blank line in a file

6. to insert a blank line

7. adding blank lines to text file

8. print contents up to blank line

9. datafile has blank lines

10. Blank lines

11. Removing data between blank lines

12. line beginning with a blank

 

 
Powered by phpBB® Forum Software