Replacing blank lines with text. 
Author Message
 Replacing blank lines with text.

Quote:

>Hiyall,

>I'm trying to replace all the blank lines in a file with some other text.
>I've tried comparisons like:  if( NF == 0 )

awk '
/^$/ {
 print "replace with"
 print "this text"
Quote:
}

/./ {
 print

Quote:
}' filename

or

sed '
/^$/c\
replace with\
this text' filename

Hope that helps,
Douglas Wilson



Mon, 25 Dec 2000 03:00:00 GMT  
 Replacing blank lines with text.

Quote:

>Hiyall,

>I'm trying to replace all the blank lines in a file with some
>other text.  I've tried comparisons like:  if( NF == 0 )

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

>but neither work.  Any ideas?

length($0) == 0  might not work if the line has spaces or tabs
on it, in which case it might not work.  The if statements must be
in the action part of the awk statement, inside the curly braces.

FWIW, it is better to include all the code that didn't work, rather
than saying what didn't work.

try these which print if the line is blank but may contain whitespace:

awk 'NF==0 {print "***This line was blank***"}' infile
awk '{if (NF==0){print "***This line was blank***"}}' infile

if you only want lines without characters:

awk '/^$/ {print "***This line has 0 characters***"}' infile
awk 'length==0 {print "***This line has 0 characters***"}' infile
awk '{if (length==0){print "***This line has 0 characters***"}}' infile

all of the above should work

BTW, length is equivalent to length($0)

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  
 Replacing blank lines with text.

Quote:



>>Hiyall,

>>I'm trying to replace all the blank lines in a file with some
>>other text.  I've tried comparisons like:  if( NF == 0 )

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

>>but neither work.  Any ideas?

>length($0) == 0  might not work if the line has spaces or tabs
>on it, in which case it might not work.  The if statements must be
>in the action part of the awk statement, inside the curly braces.

>FWIW, it is better to include all the code that didn't work, rather
>than saying what didn't work.

>try these which print if the line is blank but may contain whitespace:

>awk 'NF==0 {print "***This line was blank***"}' infile
>awk '{if (NF==0){print "***This line was blank***"}}' infile

>if you only want lines without characters:

>awk '/^$/ {print "***This line has 0 characters***"}' infile
>awk 'length==0 {print "***This line has 0 characters***"}' infile
>awk '{if (length==0){print "***This line has 0 characters***"}}' infile

>all of the above should work

>BTW, length is equivalent to length($0)

But, they only do the blamk lines.

awk 'NF==0 {print "***This line was blank***";next}{print}' infile
awk 'NF==0 {print "***This line was blank***"NF{print}' infile
or
awk 'NF==0 {print "***This line was blank***"NF>0{print}' infile

fix that, but if your awk supports the ? :  operator, I like this better:

awk '{print NF ? $0 : "***This line was blank***"}' infile

gawk supports ? :

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  
 Replacing blank lines with text.


   >Hiyall,
   >I'm trying to replace all the blank lines in a file with some other
   >text. I've tried comparisons like:  if( NF == 0 )
   >if( length($0) == 0 )
   >but neither work.  Any ideas?
   >Thanks.
   >Albert.
Either of those should work unless the file contains control characters.
Could you post the entire awk script?

Net-Tamer V 1.08X - Test Drive



Tue, 26 Dec 2000 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Using gawk to replace blank lines

2. adding blank lines to text file

3. Detecting blank lines in text file read

4. Regexp - split text by blank line.

5. Text Widget: removing blank lines

6. Search and replace text in a file based on a specific line

7. Replacing text within a line

8. Replacing a line in a Text File

9. How to replace multiple-line text

10. remove blank lines before lines beginning with string

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

12. Text widget --screen lines vs. text lines

 

 
Powered by phpBB® Forum Software