linux - Replace string in a file by matching pattern obtained from another file -


i have 2 files

file1:

$node_(17) set x_ -0.31 $node_(16) set y_ 1274.64 ns_ @ 1.0 "$node_(17) setdest -0.31 1274.64 0.00" ns_ @ 2.1 "$node_(16) setdest 2041.48 295.29 2.52" 

file2:

(17) (0) (16) (1) 

i want find first column values(of file2) in file1 , replace them corresponding 2nd column values. e.g. (17) in file1 should replaced (0), (16) (1).

output file1:

$node_(0) set x_ -0.31 $node_(1) set y_ 1274.64 ns_ @ 1.0 "$node_(0) setdest -0.31 1274.64 0.00" ns_ @ 2.1 "$node_(1) setdest 2041.48 295.29 2.52" 

how can that? thanks

with gnu sed:

sed -f <(sed 's/\((.*)\) \((.*)\)/s|\1|\2|/' file2) file1 

output:

 $node_(0) set x_ -0.31 $node_(1) set y_ 1274.64 ns_ @ 1.0 "$node_(0) setdest -0.31 1274.64 0.00" ns_ @ 2.1 "$node_(1) setdest 2041.48 295.29 2.52" 

if want edit file1 "in place" add sed's option -i.


see: the stack overflow regular expressions faq


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -