0

I am trying to modify some length code. I want to replace words in all words in list 1 with words in list 2 (pairwise).

List 1:
Vsap1*(GF/(Kagf+GF))
kdap1*AP1
vsprb
kpc1*pRB*E2F
.
.

List 2:
v1
v2
v3
v4
.
.

In other words, I'd like it to replace all instances of "Vsap1*(GF/(Kagf+GF))" with "v1" (and so on) in the file "code.txt". I have List 1 in a text file ("search_for.txt").

So far, I've been doing something like this:

set search_for=`cat search_for.txt`
set vv=1

foreach reaction $search_for
sed -i s/$reaction/$vv/g code.txt
set vv=$vv+1
end

There are many problems with this code. First, it seems the code can't handle expression with parentheses (something about "regular expressions"?). Second, I'm not sure my counter is working properly. Third, I haven't even integrated the replace list -- I thought it would be easier to just replace with 1,2,3… instead. Ideally, I would like to replace with v1,v3,v3…

Any help would be greatly appreciated!! I work mainly in Matlab (in which it is hard to deal with strings and such) so I'm not that great at csh.

Best, Mehdi

1 Answers1

0

awk should be better i think

set search_for=`cat search_for.txt`
set vindex=1

foreach reaction ${search_for}
   ReactionEscaped="`printf \"%s\" \"${reaction}\" | sed 's²[\+*./[]²\\\\&²g'`"
   sed -i "s/${ReactionEscaped}/v${vindex}/g code.txt
   let vindex+=1
end

I haven't test (no system available here) so

ReactionEscaped="printf \"%s\" \"${reaction}\" | sed 's²[\+*./[]²\\\\&²g'\"

have to be fine tuned certainly (due to double \ between "", and special meaning of car in first sed pattern) [there is lot of post about escaping special char sed pattern on the site)

NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43