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!

Help with outputing only the last value for array elements 1

Status
Not open for further replies.

naseby

Technical User
May 18, 2008
3
GB
Hello
Sorry if this is a bit simple for you folks but for this born again newbie itsn't.I thought I had posted this but cant find it, if I have sorry and I will try to delete the double up, if my first post was deleted because I dont belong here then sorry and I assume the same thing will happen to this post

I want to scan a file containing lines such as

P5050006 809 x3760
P5050007 3
P5050008 508 605 809
P5050009 57 1000
P5050010 124 596 7545 809

where S1 is a photo number and the remaining fields are shirt numbers etc appearing in that photo.

For every shirt number etc I want to list the photos in which it appears so that the output file would look something like

3 P5050007
57 P5050009
124 P5050010
596 P5050010
605 P5050008
809 P5050006 P5050008 P5050010
1000 P5050009

I am half way there via
awk ' { for (j=2; j <= NF; j++) {
rpn[$j] = rpn[$j] sprintf("%s\t", $1)
printf("%s\t %s\t %s\n",$j,(nopfr[$j] +1),rpn[ $j ])
nopfr[$j] = nopfr[$j] + 1
}
} ' file1 > file2

The obvious problem being that the output lists the sequential "rpn" values for a give shirt number eg for shirt number 809 I get
809 1 P5050006
809 2 P5050006 P5050008
809 3 P5050006 P5050008 P5050010
( ignore nopfr[$j] and $2 of the output they are just to let me see what is happening)
Really all I want to out put is the final value i.e.
809 3 P5050006 P5050008 P5050010

So if some one woyuld be kind enought to help I would be very grateful.
Thanks
 
You just need to accumulate the data first, and then do the output at the end using awk's END {} clause:

Code:
awk '
        {
                for (j=2; j <= NF; j++) {
                        rpn[$j] = rpn[$j] sprintf("%s\t", $1)
                }
        }
        END {
                for (p in rpn) {
                        printf("%s\t %s\n",p,rpn[ p ])
                }
        }
'

By defining OFS (output file separator) you can avoid the use of the (s)printf()s, and shorten it a little:

Code:
awk -v OFS='\t' '
        { for (j=2; j <= NF; j++) { rpn[$j] = rpn[$j] $1 OFS } }
        END { for (p in rpn) { print p,rpn[ p ] } }
'

Annihilannic.
 
Hi

Here on Tek-Tips we used to thank for the received help by giving stars. Please click the

* [navy]Thank Annihilannic
for this valuable post![/navy]


at the bottom of Annihilannic's post. That way you both show your gratitude and indicate this thread as helpful.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top