Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

field substitute from another file...

2 views
Skip to first unread message

nag

unread,
Nov 6, 2009, 1:45:29 AM11/6/09
to
I have two files like

file1:

id dept
123 555
123 777
123 555

321 888
321 999
321 111

231 582
231 592
231 986


file2

id dept
123 555
321 888
231 582

In file 1 id field is showing different dept numbers. But they are to
be replaced with the equalent file2 dept filed. The oupt should be
like.

id dept
123 555
123 555
123 555

321 888
321 888
321 888

231 582
231 582
231 582

I tried like this.

awk 'NR==FNR{

idfl1=$1
deptfl1=$2

1file[idfl1]=idfl1
next
}{

idfl2=$1
deptfl2=$2
if(idfl2 in 1file)
print idfl1,deptfl2
}'

but it is not giving required output

pk

unread,
Nov 6, 2009, 4:14:00 AM11/6/09
to
nag wrote:

(assuming the header line is not part of the files)

awk 'NR==FNR{dept[$1]=$2;next}{print $1, dept[$1]}' file2 file1

Ed Morton

unread,
Nov 6, 2009, 8:13:52 AM11/6/09
to
nag wrote:
<snip>

> I tried like this.
>
> awk 'NR==FNR{
>
> idfl1=$1
> deptfl1=$2
>
> 1file[idfl1]=idfl1

^ what version of awk are you running that allows variable names to
start with a number?

Ed.

nag

unread,
Nov 6, 2009, 9:41:36 AM11/6/09
to

sorry! by mistake i defined it on this post, but actually not.. thank
you

0 new messages