No output

All,
I have a small program which reads 2 text files. Text files has the below layout
dictionary
C_BOX LB_BOX
C_CHAIR LB_CHAIR
C_HAT LB_HAT

lookup
C_HAT
C_BOX
C_CHAIR

The program is below

#!/bin/bash
DICT=$2
exec < $1
while read f1
do
nawk -v fld=${f1} '/$1=fld/ { print fld $2}' $DICT
done

What I am trying is achieve is to generate a third file which will have the output.

file 3

C_HAT LB_HAT
C_BOX LB_BOX
C_CHAIR LB_CHAIR

I want to get the corresponding name and value for the "file1" fields from "dictionary". The sequence of occurence should also be maintained as it is in "file1"

thanks in advance...

Try...

 
nawk 'NR==FNR{arr[$1]=$2;next}{for(i in arr)if(i==$1)print $1,arr}' dict lookup

Try something like:

nawk -v fld="${f1}" '$1==fld { print fld" "$2}' $DICT

your rest code remains same to avoid changing the logic

Use only awk

awk 'NR==FNR{a[$1]=$0;next}{$0=a[$1]}1' dictionary lookup > file3

superb!!!! thanks guys :slight_smile:

One way to do it in Perl:

$
$ cat dictionary
C_BOX LB_BOX
C_CHAIR LB_CHAIR
C_HAT LB_HAT
$
$ cat lookup
C_HAT
C_BOX
C_CHAIR
$
$ perl -ne 'BEGIN {open(F,"dictionary");while(<F>){chomp; split; $x{$_[0]}=$_[1]} close(F)}
>           chomp; printf("%s %s\n",$_,$x{$_})' lookup
C_HAT LB_HAT
C_BOX LB_BOX
C_CHAIR LB_CHAIR
$
$

tyler_durden

something like this :

awk 'NR==FNR {a[$1]=$2;next } {if($1 in a) {b[$1]=$1" "a[$1];c[++n]=$1;next} } END { for(i=1;i<=n;i++) {print b[c]}}' file1 file2