Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ygor and others, help combining 2 Awk scripts

Status
Not open for further replies.

DanNJ

MIS
Mar 19, 2003
29
0
0
US
Ygor and others, help combining 2 Awk scripts

I want to take this script:

awk 'BEGIN {b=""}
{ if (b > "") b=b "\n" $0; else b=$0}
/Page:/ {print b;b=""}
/EOF/ {print "LINE 1 HERE\n" b;b=""}
END {print b}' < $1

And this scripts:


awk 'BEGIN {b=&quot;&quot;}
{ if (b > &quot;&quot;) b=b &quot;\n&quot; $0; else b=$0}
/Begin/ {print b;b=&quot;&quot;}
/EOF/ {print &quot;LINE 4 HERE\n&quot; b;b=&quot;&quot;}
END {print b}' < $1

And figure out how to create one script to do it all.
 

I think we need more information e.g.when do you want to print LINE 1 and when LINE 4

Here's one possiblilty.

awk 'BEGIN {b=&quot;&quot;}
{ if (b > &quot;&quot;) b=b &quot;\n&quot; $0; else b=$0}
/Page:/ {print b;b=&quot;&quot;;k=1}
/Begin/ {print b;b=&quot;&quot;;k=4}
/EOF/ {print &quot;LINE &quot; k &quot; HERE\n&quot; b;b=&quot;&quot;}
END {print b}' < $1


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
EOF is the last line of each record. There can be many records in 1 file. I want to write &quot;Line 1&quot; after the last instance of &quot;Page:&quot; in each record. I want to write &quot;Line 4&quot; after the last instance of “Begin” in each record.

&quot;Begin&quot; comes after &quot;Page:&quot;
 

Perhaps you could post some sample input data and the output you expect. Did you run the merged script and, if so, what did it do?

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
This puzzle was lot more difficult than your earlier posting. The previous solution was using a buffer &quot;b&quot; writing after EOF. I've used the same approach but I'm using an array &quot;a&quot; and variable to hold indexes for last page &quot;p&quot; and last begin &quot;b&quot;.
[tt]
awk 'BEGIN {p=0;b=0;i=0}
{a[++i]=$0}
/Page/ {p=i}
/Begin/ {b=i}
/EOF/ { for (x=1;x<=i;x++) { print a[x];
if (x==p) print &quot;LINE 1 HERE&quot;;
if (x==b) print &quot;LINE 4 HERE&quot;;
}; p=0;b=0;i=0 }' <$1
[/tt]
Note: I'm assuming that the last line in the file contains EOF.
 
Ygor,

thx again that worked great, any chance you could give me a bit of an explanation of how it works. I have been studying awk since you first helped me but this is way over my head.
 
Ygor must be taking a vacation.

# initialize variables bfore processing any lines (not strictly necessary since awk initializes variables to zero)
BEGIN {p=0;b=0;i=0}

# increment variable i and save the current line in array a
{a[++i]=$0}

# if line contains &quot;Page&quot; save current index.
/Page/ {p=i}

# if line contains &quot;Begin&quot; save current index.
/Begin/ {b=i}

# if line contains &quot;EOF&quot;, print all the save lines,
# print extra data after line containing &quot;Page&quot; and &quot;Begin&quot;,
# and initialize variables
/EOF/ { for (x=1;x<=i;x++) { print a[x];
if (x==p) print &quot;LINE 1 HERE&quot;;
if (x==b) print &quot;LINE 4 HERE&quot;;
}; p=0;b=0;i=0 }


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
I have indeed been on vacation. Thanks to CaKiwi for kindly providing an explanation of my code. I am also pleased that awk has a new student, DanNJ.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top