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.