Modifying existing script to remove trailing newlines 
Author Message
 Modifying existing script to remove trailing newlines

I have two scripts that I want to combine.  The first I got from a
post made to comp.lang.awk by Ian Stirling:

gawk 'BEGIN{RS="\n+";ORS=""}x{print x}x=RT' infile

It removes trailing newlines from a file.

The other script removes "Received:" headers from an RFC822 message
and appends a new header:

============================================================
# init our variables
BEGIN { bInHeaders = 1 }

bInHeaders {
    if (/^Received:/) {
        in_received_header = 1
        next       # skip first line of Received: headers
    } else if (in_received_header) {
        if (/^[[:space:]]/) {
            next   # skip second and subsequent lines
                   # of Received: headers
        } else {
            in_received_header = 0
        }#else
    }#else if

    # if we've hit an empty line, then this must be the end
    # of the headers
    if (! length)
    {
        # Add the header and set the flag variable
        print "X-NewHeader: header"; bInHeaders = 0
    }#if

Quote:
}#bInHeaders

# If we've gotten past the headers, then just keep every line
{ print }

============================================================

I want to modify the second script to remove trailing newlines as
well.  The problem is that it has to retain blank lines that appear
before the last non-blank line.

I've tried a few variations of setting FS and OFS in various places,
but I've either introduced extraneous blank lines or removed ones I
wanted to keep.  I think that my problem is that I can't quite picture
how to perform one default action while bInHeaders == true and another
when == false.

--
Charles Calvert             |  Software Design/Development
Celtic Wolf, Inc.           |  Project Management
http://www.*-*-*.com/ ;|  Technical Writing
(703) 580-0210              |  Research



Sat, 08 May 2004 01:45:56 GMT  
 Modifying existing script to remove trailing newlines

Quote:

> I have two scripts that I want to combine.  The first I got from a
> post made to comp.lang.awk by Ian Stirling:

> gawk 'BEGIN{RS="\n+";ORS=""}x{print x}x=RT' infile

> It removes trailing newlines from a file.

shorter:  awk  '{printf "%s",$0}'

Quote:

> The other script removes "Received:" headers from an RFC822 message
> and appends a new header:

[snipped]

Quote:
> I want to modify the second script to remove trailing newlines as
> well.  The problem is that it has to retain blank lines that appear
> before the last non-blank line.

> I've tried a few variations of setting FS and OFS in various places,
> but I've either introduced extraneous blank lines or removed ones I
> wanted to keep.  I think that my problem is that I can't quite picture
> how to perform one default action while bInHeaders == true and another
> when == false.

simplest solution:   use the 'printf' command to take control, e.g.
i)  printf "%s\n", "line with a trailing newline"
ii) printf "%s"  , "line with no trailing newline"

man awk   /  printf

Regards Torfinn



Sun, 09 May 2004 16:38:28 GMT  
 Modifying existing script to remove trailing newlines
Quote:


...
>>post made to comp.lang.awk by Ian Stirling:

>>gawk 'BEGIN{RS="\n+";ORS=""}x{print x}x=RT' infile

>>It removes trailing newlines from a file.

>shorter:  awk  '{printf "%s",$0}'

...

They don't do the same thing. In the first script after the BEGIN
block, on the first record x == "", so the {print x} isn't run, but
the x=RT performs the implicit print of the first record. On the
second record, x != "", so {print x} runs and prints the record
terminator from the previous record. Then the second record is
printed. This bit of cleverness fails if the input file has no ending
newline after the final record because RT == "", so x=RT evaluates
false. The second script strips off ALL newlines.

One POSIX-only way to do this that works whether or not there's a
newline at the end of the input would be

!$0 { t = t "\n"; next } { printf "%s%s", t, $0; t = "\n" }



Mon, 10 May 2004 18:03:57 GMT  
 Modifying existing script to remove trailing newlines

Quote:



> ...
> >>post made to comp.lang.awk by Ian Stirling:

> >>gawk 'BEGIN{RS="\n+";ORS=""}x{print x}x=RT' infile

> >>It removes trailing newlines from a file.

> >shorter:  awk  '{printf "%s",$0}'
> ...

> They don't do the same thing. In the first script after the BEGIN
> block, on the first record x == "", so the {print x} isn't run, but
> the x=RT performs the implicit print of the first record. On the
> second record, x != "", so {print x} runs and prints the record
> terminator from the previous record. Then the second record is
> printed. This bit of cleverness fails if the input file has no ending
> newline after the final record because RT == "", so x=RT evaluates
> false. The second script strips off ALL newlines.

Thanks for setting me straight, I see now that I failed the subject.
Torfinn

- Show quoted text -

Quote:

> One POSIX-only way to do this that works whether or not there's a
> newline at the end of the input would be

> !$0 { t = t "\n"; next } { printf "%s%s", t, $0; t = "\n" }



Tue, 11 May 2004 16:32:10 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Remove trailing newlines from file

2. $canvas find enclosed does not find text with trailing newline

3. trailing newline in string.splitfields()

4. removing trailing spaces from string variables

5. Removing trailing spaces from a field using awk

6. Removing trailing blanks

7. how to remove trailing blanks of a string?

8. Elisp--Remove a trailing NULL char?

9. How to remove newlines in text file

10. Removing final newline from a Tcl string using C API

11. Adding New VI's to existing library and modifying .mnu files

12. Problem modifying existing code!

 

 
Powered by phpBB® Forum Software