Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi Gurus. Can anyone help me with the below requirement? I need Unix shell script for that.
I am devastated, if you help me I'm really grateful to you.
Table 1:
Admin 10 20
Sports 20 19
Library 12 3Table 2:
Admin 25 26
Dean 12 13
Library 11 22I need output as in table3??
Table 3:Admin 10 20 25 26
Dean NA NA 12 13
Library 12 3 11 22
Sports 20 19 NA NAIf any questions plz ask me. I really appreciate you for considering this.

Thanks for ur response. I actually tried with "awk". But I never
be on track and I dint know how to put "NA".Can anyone helpme?
A will always finds a way.

if you have Python,
#!/usr/bin/env python data=open("file1").read().split("\n") d={} for item in data: i=item.split() d[i[0]]=i[1:3] for line in open("file2"): oline= line.strip() oline=oline.split() if d.has_key(oline[0]): print ' '.join(oline),' '.join(d[oline[0]]) del d[oline[0]] else: print ' '.join(oline[0:3]),"NA NA " for i,j in d.iteritems(): print i, ' '.join(j),"NA NA"
output
# ./test.py
Admin 25 26 10 20
Dean 12 13 NA NA
Library 11 22 12 3
Sports 20 19 NA NA

Hi Friend,
I will be grateful to your response.
Here the required solution is different from the answer given here. See the "NA" in 2nd & 4th row.
Moreover, I tried with "awk" alot. But didnt get the required output yet. I will appreciate if your response is in unix shell script.
Thankyou very much for considering my situation.
A will always finds a way.

awk version is nawk since I'm using Solaris:
#!/bin/ksh
nawk ' BEGIN { while ( getline < "admin1.txt" > 0 ) myarr[$1]=$2" "$3 } { if($1 in myarr) { printf("%s %s %s %s\n", $1, myarr[$1], $2, $3) # delete the array element after printing delete myarr[$1] } else printf("%s NA NA %s %s\n", $1, $2, $3) } END { # print any array elments that were not matched for (i in myarr) printf("%s %s NA NA\n", i, myarr[i]) } ' admin2.txt

Hi,
I found out, by using "join" we can easily solve this.
see "man join" helps a lot.
Ghostdog & Nails... My special Thanks to you. Your
suggestions helped me lot.A will always finds a way.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |