Help with search string between two files

Hi,

Basically i want to search for a string in file two based on the input file one and if it matches get the nextline and print the value of the field name.

cat one

abc
xyz
def

cat two

<src>
<name="path/to/abc" test="value_version">
<new name="Y2" >
  </src>
<src>
<name="path/to/xyz" test="value_version">
<new name="Y1" >
  </src>

expected output:

abc 
Y2
xyz 
Y1

I tried like this ( i know its a ugly one):

while read line ; do echo "$line"; echo "$line" | awk -F\" '{getline;print $2;}' two ; done < one

I appreciate your help :b:

Try:

awk 'NR==FNR{A[$1];next}/name=/ && getline && $NF in A{print $NF;while(! /new name=/)getline;getline;print $NF}' one RS=\" FS=/ two

This should be a bit robust..

One way would be..

awk -F\" 'NR==FNR{a[$1]=$1;next}{for(i in a)if($2~i){getline;print i"\n"$2}}' file_1 file_2

Hi,

Thanks to both of you .

@michaelrozar17:
It works as expected with exception.
what if i have name as follows:

abc_1
abc
abc_spl_val

i get name value for abc_1 in place of abc because of $2~i i guess.

try $2 == i

Ok. Try as

[mkt@michael]$ cat file_1
abc
xyz
abc_spl_val
def
abc_1
[mkt@michael]$ awk -F\" 'NR==FNR{a[++i]=$1;next}{for(i in a){sub(".*/","",$2);if($2==a){getline;print a"\n"$2;break}}}' file_1 file_2