Need to split file with two reports per line 
Author Message
 Need to split file with two reports per line

Hello,

I have a text file where two different reports
span across the same line.  For example:

Report #1 line 1.......    Report #2 line 1......
Report #1 line 2.......    Report #2 line 2......
^M^L
Report #3 line 1.......    Report #4 line 1......
Report #3 line 2.......    Report #4 line 2......

etc.

I need to print Report #1 followed by Report #2
followed by Report #3, etc.  For each line of
input, I've tried using substr to print the left
report while appending the right to the hold
space.  Once I detect ^M^L, I move the hold space
back into the pattern space for printing.

The Hold command and get commands are not working
as expected.  Any help appreciated!!!

Thanks.

Sent via Deja.com http://www.*-*-*.com/
Share what you know. Learn what you don't.



Tue, 18 Dec 2001 03:00:00 GMT  
 Need to split file with two reports per line

Quote:

> I have a text file where two different reports
> span across the same line. For example:

> Report #1 line 1.......    Report #2 line 1......
> Report #1 line 2.......    Report #2 line 2......
> ^M^L
> Report #3 line 1.......    Report #4 line 1......
> Report #3 line 2.......    Report #4 line 2......

> etc.

> I need to print Report #1 followed by Report #2
> followed by Report #3, etc. For each line of
> input, I've tried using substr to print the left
> report while appending the right to the hold
> space. Once I detect ^M^L, I move the hold space
> back into the pattern space for printing.

> The Hold command and get commands are not working
> as expected. Any help appreciated!!!

Awk doesn't have a "hold space" or a "pattern space," nor does it
have commands named "H(old)" or "g(et)"; sed does. Conversely, sed
doesn't have a "substr" command; awk does. I think you're confusing
awk and sed.

Since this is comp.lang.awk, and since doing what you want to do
in sed would be excruciating (to read if not to write), I'll offer
only an awk suggestion.

Let's assume that the left column of report data is within character
positions 1 through 27, and that the right column of report data
begins in character position 28 and continues to the end of the
line. Let's also assume that the end of each report pair is terminated
by a line containing only the two-character string "\r\f" ("^M^L",
a carriage return and a formfeed).

# untested
{
    if ($1 == "\r\f") {
        for (i = 1; i <= idx; i++)
            print buffer[i]
        idx = 0
    } else {
        print rtrim(substr($0, 1, 27))
        buffer[++idx] = rtrim(substr($0, 28))
    }

Quote:
}

END {
# in case the last report pair isn't terminated with "\r\f"
    for (i = 1; i <= idx; i++)
        print buffer[i]

Quote:
}

function rtrim(str) {
    sub(/ +$/, "", str)
    return str

Quote:
}

If the length of the report in the left column is not the same as
the length of the report in the right column, this script will
print blank lines at the end of the shorter report to "even them
out."

--
Jim Monty

Tempe, Arizona USA



Tue, 18 Dec 2001 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. How to compare two files line-by-line

2. The pretty-printer, *print-lines* and per-line prefixes

3. one liner? --- split file at empty line

4. Parsing files and lines using split

5. Splitting lines in a file

6. how to split each new line into a list, considering quoted new line characters

7. read two sets of data from two spread sheet files to two arrays problem

8. Writing two equal files without creating two files in dct

9. Need Two Report Headers - How???

10. HowTo? : convert file, take two line parts

11. Ranges in AWK (file name is two lines down)

12. Newbie needs help splitting large file

 

 
Powered by phpBB® Forum Software