How to pick values from column based on key values by usin AWK

Dear Guyz:)

I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z).
I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in second column) by using the following.

But in this case inputfile1 is different , the keys are just not in single column but also in other columns.
Could you please help me on this.

Inputfile1

A tab F tab N
D
X tab Z

Inputfile2

A tab 1200
B tab 1000
D tab 3000
F tab AHAHA
G tab XXXXX
N tab YYYYY
X tab EEEEE
Z tab GGGG

I would like to join the the values or letters based on keys in 1st input file1
(A,F,N,D,X,Z) like the following

Output
A \t 1200 \t F \t AHAHA \t N \t YYYYY
D \t 3000
X \t EEEEE Z \t GGGG

Try:

while read line
do
   s=""
   for i in $line
   do
      s=$s"\t"$(grep -w $i input2.txt)
   done
   echo $s
done < input1.txt

Tested:

  A 1200 F AHAHA N YYYYY
  D 3000
  X EEEEE Z GGGG

:slight_smile:
Thanx alot I will try it

Try this:

> fileout.txt
while read line ; do
   xline='^'$(echo "$line" | sed 's/\t/|^/g')
   echo "$(echo "$(egrep "$xline" Inputfile2)"| tr '\n' '\t')"  >> fileout.txt
done < Inputfile1
nawk 'NR==FNR{_[$1]=$2}NR!=FNR{for(i=1;i<=NF;i++){printf("%s %s ",$i,_[$i])}print ""}' file1 file2

Guys I have a Problem . Sorry for troubling.
I think you misunderstood me.
he output 1st row has to contain 3 keys as I mentioned (A, F and N)and their values
and 2nd row 1 key D and its value
3rd row ..............................................

while read line
do
   s=""
   for i in $line
   do
      s=$s"\t"$(grep -w $i input2.txt)
   done
   echo $s
done < input1.txt

Its working fine but not merging lines into a single line with tabs.like

others

while read line ; do
xline='^'$(echo "$line" | sed 's/\t/|^/g')
echo "$(echo "$(egrep "$xline" Inputfile2)"| tr '\n' '\t')" >> fileout.txt
done < Inputfile1

I didn't find any output

@cherry

nawk 'NR==FNR{_[$1]=$2}NR!=FNR{for(i=1;i<=NF;i++){printf("%s %s ",$i,_[$i])}print ""}' file1 file2

same problem like the 1st one. all values in seperate line like
A 1200
F AHAHA
N YYYYY
D 3000
X EEEEE
Z GGGG

Just swap the input files:

nawk ... file2 file1

I did its giving correct values but they are like this. the set I need ending with new line and placed in different rows instead of single line

I think I can convert and replace symbols and newlines but is there any possibility in this code to get desired result

I'm getting the correct result. Could you attach samples of your input files?

thanx in advance
and I'm using macbook (may be format of the file I have impoted from windows os (\n or \r))
apologies if your code is correct
I'm sorry the internet is bit slow . my files are not uploading.

I got the correct output and I pasted that here. strange!!!

I suppose this is the output file, could you attach/upload even smaller samples from your input files (Inputfile1 and Inputfile2)?

Anyway, the next thing you can try is to run dos2unix on both input files before processing them. What version of Mac OS you're using?

macosX
dow2unix filename
si it right??

It's dos2unix.

dos2unix filename

the files not uploadin enethough I made smaller files around 60kb
Sorry and Thanx alot for suggestions
really helpful

Here is my code again.

> fileout.txt
while read line ; do
xline='^'$(echo "$line" | sed 's/\t/|^/g')
echo "$(echo "$(egrep "$xline" Inputfile2)"| tr '\n' '\t')" >> fileout.txt
done < Inputfile1
cat fileout.txt

It works correctly for me.
Here is the result I got.

A 1200 F AHAHA N YYYYY
D 3000
X EEEEE Z GGGG

Thank you Guys Almost every script working fine