简体   繁体   中英

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

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.

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

不幸的是,这需要对文件进行排序:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM