remove blank lines before lines beginning with string 
Author Message
 remove blank lines before lines beginning with string


Quote:

>How can I remove blank lines in a file that appear just before a line that
>begins with a certain string? For example, in the lines below,
>I would like to remove the blank lines preceding lines beginning with
>"author:".

>Vivek Rao

>*****************************************************

>title: Great Expectations

>author:{*filter*}ens

>title: Wealth of Nations

>author: Adam Smith

!NF && getline && !/^author/ { print "" }
3


Thu, 22 May 2003 03:00:00 GMT  
 remove blank lines before lines beginning with string
How can I remove blank lines in a file that appear just before a line that
begins with a certain string? For example, in the lines below,
I would like to remove the blank lines preceding lines beginning with
"author:".

Vivek Rao

*****************************************************

title: Great Expectations

author:{*filter*}ens

title: Wealth of Nations

author: Adam Smith



Fri, 23 May 2003 09:35:41 GMT  
 remove blank lines before lines beginning with string

Quote:



>>How can I remove blank lines in a file that appear just before a line
>>that begins with a certain string? For example, in the lines below,
>>I would like to remove the blank lines preceding lines beginning with
>>"author:".
...
>>*****************************************************

>>title: Great Expectations

>>author:{*filter*}ens

>>title: Wealth of Nations

>>author: Adam Smith

>!NF && getline && !/^author/ { print "" }

This script only prints the blank line immediately preceding any line
that doesn't begin with 'author'. It doesn't print the title or author
lines or the blank lines preceding title lines. Doesn't seem what the
OP would want.

The OP's example is easy, but the problem statement is broader. What
happens if the line starting with 'author:' is preceded by multiple
blank lines? How about

!NF { ++b; next }
{ if (b && !/^author: /) { while (b-- > 0) print ""}; b = 0 ; print }

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Fri, 23 May 2003 03:00:00 GMT  
 remove blank lines before lines beginning with string
The following code will do what you want.

!NF {count++}
/^author/ {print $0; count=0}
NF && !/^author/ { while (count >0) {print ""; count--}
   print $0 }


Quote:
> How can I remove blank lines in a file that appear just before a line that
> begins with a certain string? For example, in the lines below,
> I would like to remove the blank lines preceding lines beginning with
> "author:".

> Vivek Rao

> *****************************************************

> title: Great Expectations

> author:{*filter*}ens

> title: Wealth of Nations

> author: Adam Smith



Fri, 23 May 2003 03:00:00 GMT  
 remove blank lines before lines beginning with string

Quote:





> >>How can I remove blank lines in a file that appear just before a line
> >>that begins with a certain string? For example, in the lines below,
> >>I would like to remove the blank lines preceding lines beginning with
> >>"author:".
> ...
> >>*****************************************************

> >>title: Great Expectations

> >>author:{*filter*}ens

> >>title: Wealth of Nations

> >>author: Adam Smith

> >!NF && getline && !/^author/ { print "" }

> This script only prints the blank line immediately preceding any line
> that doesn't begin with 'author'. It doesn't print the title or author
> lines or the blank lines preceding title lines. Doesn't seem what the
> OP would want.

I think Kenny's script had that "3" on the second line which
was intended to be part of it. That would then make the script
work, being equivalent to:

!NF && getline && !/^author/ { print "" };1

Why a 3 I don't know ...

Tristan.



Fri, 23 May 2003 03:00:00 GMT  
 remove blank lines before lines beginning with string


Quote:

>>!NF && getline && !/^author/ { print "" }

You clipped the most important line!  Go back and check the original.
Your apology is accepted in advance.

Your point about the multiple blank lines is, of course, valid, but it is
sheer speculation on all of our parts as to whether or not that is relevant
to the OP's problem (I.e., we don't know how well defined his data is).

Another, simpler, way to do it follows (note that this will squeeze multiple
blank lines into 1 - which may well be desirable, when all is said and done):

BEGIN {RS=""}
!/^author/ { print "" }
.007

Oh, and by the way, I had problems with this gawk command line:

        gawk '{print NR,$0}' RS=""

This does the right thing unless the first thing you type in as input
is a blank line - this seems to cause it to hang.  It looks like it
can't handle the first record being null (when RS is null).  This looks
like a bug to me - can anyone confirm?



Fri, 23 May 2003 03:00:00 GMT  
 remove blank lines before lines beginning with string

Quote:



>>>!NF && getline && !/^author/ { print "" }

>You clipped the most important line!  Go back and check the original.
>Your apology is accepted in advance.

Yes, sorry. I was blinded by your cleverness.

...

Quote:
>Another, simpler, way to do it follows (note that this will squeeze
>multiple blank lines into 1 - which may well be desirable, when all is
>said and done):

> BEGIN {RS=""}
> !/^author/ { print "" }
> .007

If input begins with a nonblank line, this new script prints a blank
line before the first line on output; also, if input ends with a series
of blank lines, they're eliminated on output.

The clever (evil?) way to do this with gawk would be

BEGIN { RS = "||"; FS = "\n+author: "; OFS = "\nauthor: " }
{ $1 = $1; print }

Note: the RS = "||" makes the entire contents of each file a single
record. Probably dangerous with other awks.

Quote:
>Oh, and by the way, I had problems with this gawk command line:

>    gawk '{print NR,$0}' RS=""

>This does the right thing unless the first thing you type in as input
>is a blank line - this seems to cause it to hang.  It looks like it
>can't handle the first record being null (when RS is null).  This looks
>like a bug to me - can anyone confirm?

gawk -v RS="" '{ print NR, $0 }'

also appears to hang, but the problem is more subtle for gawk 3.0.4
under WinNT. Note: both scriopts work if input comes from a file, but
it doesn't seem to work if input comes from the keyboard.

Using gawk 3.0.4 under WinNT, I get weird results: if I enter a blank
line then abc, then a blank line, the 'abc' and following blank line
aren't echoed to the display, and when I press Ctrl+C (Ctrl+Z doesn't
end input) to terminate gawk, I get a 'DOS' prompt followed by abc on
one line followed by the NT error message: "The name specified is not
recognized as an internal or external command, operable program or
batch file."

For this particular gawk version under WinNT, it appears that if (1)
input comes from the keyboard, (2) the first input line is a blank
line, (3) RS is set to "" either before or after reading the first
input line, then subsequent lines are effectively queued in gawk's
console input buffer, and when gawk is terminated with Ctrl+C, these
queued lines are read by NT's command processor.

Looks like a bug to me.

Sent via Deja.com http://www.deja.com/
Before you buy.



Fri, 23 May 2003 03:00:00 GMT  
 remove blank lines before lines beginning with string

Quote:

> How can I remove blank lines in a file that appear just before
> a line that begins with a certain string? For example, in the
> lines below, I would like to remove the blank lines preceding
> lines beginning with "author:".

This is easily accomplished with a simple substitution in Perl:

$ cat booklist

title: Great Expectations

author:{*filter*}ens

title: Wealth of Nations

author: Adam Smith

$ perl -p0777e 's/\n+(?=\nauthor:)//g' booklist

title: Great Expectations
author:{*filter*}ens

title: Wealth of Nations
author: Adam Smith

$ perl -p0777e 's/\s+(?=\nauthor:)//g' booklist

title: Great Expectations
author:{*filter*}ens

title: Wealth of Nations
author: Adam Smith

$

The second example, only slightly different than the first, will
remove all "blankish" lines (i.e., lines consisting of only tabs,
spaces, formfeeds, etc.), as well as trim trailing whitespace
characters from the end of the title: line.

Another suggestion: If you choose to program a solution in awk,
avoid needless obfuscation such as

    3

instead of the more usual and obvious

    { print }

This will avoid confusion.

--
Jim Monty

Tempe, Arizona USA



Sat, 24 May 2003 08:26:12 GMT  
 remove blank lines before lines beginning with string

Quote:

> How can I remove blank lines in a file that appear just before
> a line that begins with a certain string?

[The following script was tested using GNU Awk version 3.0.4.]

#!gawk -f
#
# squash.awk -- delete blank lines that precede a line matching a pattern,
#               carefully preserving blank lines that do not

BEGIN {
    tag_pattern = "^author:"

Quote:
}

{
    if (NF) {
        if (blank_lines) {
            if ($0 !~ tag_pattern) {
                printf("%s", blank_lines)
            }
            blank_lines = ""
        }
        print
    } else {
        blank_lines = blank_lines "\n" $0
    }

Quote:
}

END {
    if (blank_lines) {
        printf("%s", blank_lines)
    }

Quote:
}

This simple, straightforward awk script exploits the fact that NF
is true for all non-blank lines (i.e., lines that have something
other than just whitespace characters on them). It carefully
preserves all wanted blank lines, including those that have whitespace
characters on them. It only performs the regular expression pattern
match operation on non-blank lines that follow blank lines, and it
only initializes the value of the variable blank_lines to the null
string when it is not already initialized. It also prints any blank
lines that occur at the end of all input.

This awk script can easily be modified to adapt it to changing
requirements.

--
Jim Monty

Tempe, Arizona USA



Sat, 24 May 2003 09:33:03 GMT  
 
 [ 9 post ] 

 Relevant Pages 

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

2. line beginning with a blank

3. Removing data between blank lines

4. Want to remove multiple occurances of blank line from a file

5. Remove lines beginning with # from file

6. remove blank lines

7. Text Widget: removing blank lines

8. how to remove trailing blanks of a string?

9. inserting a blank line in a file

10. to insert a blank line

11. adding blank lines to text file

12. print contents up to blank line

 

 
Powered by phpBB® Forum Software