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

Printing array elements problem

Status
Not open for further replies.

mrr

Technical User
May 3, 2001
67
US
I have an awk script as:
BEGIN { FS = " " }
NR == 1 {
prev = $3
}
$3 == prev {
++count
lines[++i] = $1","$2
}
$3 != prev {
print count",1"
for ( j =1; j <= i; j++)
print lines[j]
for (all in lines)
delete lines[all]
prev = $3
i = 0
lines[++i] = $1&quot;,&quot;$2
count = 1
}
which takes a file such as:
1111 2221 1 3331 44441 55551
1112 2222 1 3332 44441 55551
1113 2223 1 3333 44441 55551
1111 2221 2 3331 44442 55552
1112 2222 2 3332 44442 55552
1113 2223 2 3333 44442 55552
1111 2221 3 3331 44443 55553
1112 2222 3 3332 44443 55553
1113 2223 3 3333 44443 55553
and builds a file with total number of points where field 3 is similar and gives a header.

The output is:
3,1
1111,2221
1112,2222
1113,2223
3,1
1111,2221
1112,2222
1113,2223
3,1
1111,2221
1112,2222
1113,2223

How can I include on the first record of each group of data elements from the 5th & 6th field and create this type of file?
3,1,&quot;44441&quot;,&quot;55551&quot;
1111,2221
1112,2222
1113,2223
3,1,&quot;44442&quot;,&quot;55552&quot;
1111,2221
1112,2222
1113,2223
3,1,&quot;44443&quot;,&quot;55553&quot;
1111,2221
1112,2222
1113,2223

Thanks
 
BEGIN { FS = &quot; &quot; }
NR == 1 {
prev = $3; inf=&quot;\&quot;&quot;$5&quot;\&quot;,\&quot;&quot;$6&quot;\&quot;&quot;
}
$3 == prev {
++count
lines[++i] = $1&quot;,&quot;$2
}
$3 != prev {
print count&quot;,1,&quot;inf
for ( j =1; j <= i; j++)
print lines[j]
for (all in lines)
delete lines[all]
prev = $3; inf=&quot;\&quot;&quot;$5&quot;\&quot;,\&quot;&quot;$6&quot;\&quot;&quot;
i = 0
lines[++i] = $1&quot;,&quot;$2
count = 1
}
Don't you need a END section for the last group ?

Hope This Help
PH.
 
[tt][ignore]
function PrintGroup( i) {
print count &quot;,1,&quot; inf;
for ( i =1; i <= count; i++) {
print lines;
delete lines;
}
}

$3 != prev {
if (NR > 1) PrintGroup();
count = 0
inf = &quot;\&quot;&quot;$5&quot;\&quot;,\&quot;&quot;$6&quot;\&quot;&quot;;
prev = $3;
}

{
lines[++count] = $1 &quot;,&quot; $2;
}

END {
PrintGroup();
}
[/ignore][/tt]

Jean Pierre.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top