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!

Reformat multiple records into one 1

Status
Not open for further replies.

learningawk

Technical User
Oct 15, 2002
36
US
I'm having a slight problem with reformatting a data file.
Here's the input:
624 1010
0 4917
50 4921
100 4924
624 1042
0 4917
50 4921
100 4921
150 4924
624 1074
0 4895
50 4898
100 4901
150 4901
200 4904

and here's the script I've been using to group data together:
{
test1=substr($0,1,5);
if (match( test1, "[0-9]" )) {f1=substr($0,1,5); f2=substr($0,10,6)}
if (match( test1, "^[ ]*$" )) {f3=substr($0,10,8); f4=substr($0,20,8)}
print f1,f2,f3,f4
}

and here's the output that I get:
624 1010
624 1010 0 4917
624 1010 50 4921
624 1010 100 4924
624 1042 100 4924
624 1042 0 4917
624 1042 50 4921
624 1042 100 4921
624 1042 150 4924
624 1074 150 4924
624 1074 0 4895
624 1074 50 4898
624 1074 100 4901
624 1074 150 4901
624 1074 200 4904

You can notice the output is off on the first record and then the following records are off.

I would like to:
1. Correct this problem so it prints all 4 correct fields on the same line.
and also I would like to change script to a different ouput which would:
2. Change the output so that it only print the f1 and f2 on only the first group of records like this:

624 1010 0 4917
50 4921
100 4924
624 1042 0 4917
50 4921
100 4921
150 4924
624 1074 0 4895
50 4898
100 4901
150 4901
200 4904

Thanks alot for helping.
 
read the values in an array, then in 'END' section
print them ! -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
When substring 1 and substring 2 are both non-null and not whitespace then printf without a new line and then use getline to print the strings 3 and 4 (concat of 1 2 3 & 4). The formatting is up to you. Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
Could you help a little more on the reformat on data output #2? I am trying (with not much success) to get the format to look like:
624 1010 0 4917
50 4921
100 4924
624 1042 0 4917
50 4921
100 4921
150 4924
624 1074 0 4895
50 4898
100 4901
150 4901
200 4904
Thanks
 
I have revised my script and have accomplished my desired output of:
624-1010 0 4917
50 4921
100 4924
150 4924
200 4927
624-1042 0 4917
50 4921
100 4921
150 4924
624-1074 0 4895
50 4898

with the input of:
624 1010
0 4917
50 4921
100 4924
150 4924
200 4927
624 1042
0 4917
50 4921
100 4921
150 4924
624 1074
0 4895
50 4898

Here's my final script to accomplish it:

#!/usr/bin/nawk -f
{
test1=substr($0,1,5);
if (match( test1,"[0-9]" )) {count=0;printf ("%4s%s%-4s",$1,"-",$2)}
else
if (!match( test1,"[0-9]" )) {
++count;
f3=substr($0,13,5);
f4=substr($0,23,5);
if (count==1) {printf ("%31s%s%5s\n",f3," ",f4)}
if (count>1) {printf ("%40s%s%5s\n",f3," ",f4)}
}
}

note:columns 3 and 4 are right justified and columnar.

Does anyone have any ideas how this might have been done more efficiently or easier?
 
Your solution seems fine, but here is how I would have done it.

#!/usr/bin/nawk -f
{
if (substr($0,1,5) != " ") {
printf ("%4s-%-4s",$1,$2)
count = 31
next
}
s = "%" count "s %5s\n"
printf (s, substr($0,13,5), substr($0,23,5))
count = 40
} CaKiwi
 
CaKiwi,

Thanks for a different perspective, I appreciate it.

Also, thanks to jamisar and bigoldbulldog for your help.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top