webminal.org
webminal.org

Reputation: 47206

shell script cut and sed help

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

Upvotes: 0

Views: 1012

Answers (3)

Dennis Williamson
Dennis Williamson

Reputation: 360105

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

Upvotes: 0

ghostdog74
ghostdog74

Reputation: 342373

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

Upvotes: 2

paxdiablo
paxdiablo

Reputation: 881523

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.

Upvotes: 4

Related Questions