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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

format problem 1

Status
Not open for further replies.

learningawk

Technical User
Oct 15, 2002
36
US
I have a data file as:
H1
H2
H3
H4 0 0 0 0
1980 480 607006 3030970 0
100 1495 200 1495 300 1495 400 1495
500 1495 600 1495 700 1495 800 1495
3300 2096 3400 2128 3500 2200
END
1980 512 607806 3030970 0
100 1498 200 1498 300 1498 400 1498
500 1498 600 1498 700 1498 800 1498
3300 2053 3400 2095
END
1980 544 608606 3030970 0
100 1494 200 1494 300 1494 400 1494
500 1494 600 1494 700 1494 800 1494
900 1494 1000 1494 1100 1494 1200 1495
3300 2048
END

and here's my current script to reformat:

BEGIN {
print "H...+....1....+....2....+....3....+....4....+....5"
}
{
# if (index(substr($0,1,1),"H") || (index(substr($0,1,1),"h"))) next;
if (NR<5) next;
if (match(substr($0,1,4),/[0-9]/) && match(substr($0,21,4),/[0-9]/)) {in_line=substr($0,1,4);cross_line=substr($0,21,4);next}
time=$1;
velocity=$2;
printf(&quot;%4d%s%-4d%31d%7d\n&quot;,in_line,&quot;-&quot;,cross_line,time,velocity)
while (!match(substr($0,1,3),&quot;END&quot;))
{
printf(&quot;%40d%7d\n&quot;,$1,$2);
printf(&quot;%40d%7d\n&quot;,$3,$4);
printf(&quot;%40d%7d\n&quot;,$5,$6);
printf(&quot;%40d%7d\n&quot;,$7,$8);getline
}
}

and here's my current output:

H...+....1....+....2....+....3....+....4....+....5
1980-480 100 1495
100 1495
200 1495
300 1495
400 1495
500 1495
600 1495
700 1495
800 1495
3300 2096
3400 2128
3500 2200
0 0
1980-512 100 1498
100 1498
200 1498
300 1498
400 1498
500 1498
600 1498
700 1498
800 1498
3300 2053
3400 2095
0 0
0 0
1980-544 100 1494
100 1494
200 1494
300 1494
400 1494
500 1494
600 1494
700 1494
800 1494
900 1494
1000 1494
1100 1494
1200 1495
3300 2048
0 0
0 0
0 0

Now here's my problem:
How can I keep the script from repeating the first pair of values after it starts a new group of records. Also, how do you keep from printing the zeros when there is a blank or null field being read. The input file does not always have all 8 columns of value data present.

I would also like to see an alternate way of scripting this problem for a different approach.
Thanks,
 
This is not tested.
...
printf(&quot;%4d%s%-4d%31d%7d\n&quot;,in_line,&quot;-&quot;,cross_line,time,velocity)
getline
while (!match(substr($0,1,3),&quot;END&quot;))
{
if (NF>=1) printf(&quot;%40d&quot;,$1)
if (NF>=2) printf(&quot;%7d\n&quot;,$2)
if (NF>=3) printf(&quot;%40d&quot;,$3)
if (NF>=4) printf(&quot;%7d\n&quot;,$4)
if (NF>=5) printf(&quot;%40d&quot;,$5)
if (NF>=6) printf(&quot;%7d\n&quot;,$6)
if (NF>=7) printf(&quot;%40d&quot;,$7)
if (NF>=8) printf(&quot;%7d\n&quot;,$8)
getline
}
}


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top