views:

59

answers:

3

I have 2 two files

$cat file1.txt
 field1=value1
 field2=value2
 field3=value3
 ::
 ::

 $cat file2.txt
 something.field1.some
 otherthing.field2.anything
 anything.field3.something

I need to read file1.txt and check whether file2.txt for fieldN and replace with valueN

so that the result will be

  something.value1.some
  otherthing.value2.anything
  anything.value3.something
+4  A: 

Provided there are no special sed-type characters in your fields and values, you can use a meta-sed approach:

pax> sed -e 's/^/s\/\\./' -e 's/=/\\.\/./' -e 's/$/.\/g/' file1.txt >x.sed
pax> sed -f x.sed file2.txt

something.value1.some
otherthing.value2.anything
anything.value3.something

If you look at the x.sed file, you'll see that the first sed just makes a list of sed commands to be executed on your second file.

paxdiablo
thanks..i'll try this .
lakshmipathi
+2  A: 

use awk

$ awk -F"[=.]" 'FNR==NR{a[$1]=$2;next}{$2=a[$2]}1' OFS="." file1 file2
something.value1.some
otherthing.value2.anything
anything.value3.something
ghostdog74
thank you for your answer,I'll use this method after learning about awk. i know something about sed but zero at awk :) thanks again.
lakshmipathi
A: 

This unfortunately requires the files to be sorted:

tr = . < file1.txt | join -t . -1 1 -2 2 -o 2.1 1.2 2.3 - file2.txt
Dennis Williamson