Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to parse bunch of files and replace each occurance of __esct function?

__esct('Full name')   

convert to:

$this->escape($this->translate('Full name'))  

I'm confused as to - how to add that second brace after replaced text?

share|improve this question
up vote 3 down vote accepted

Try using this :

sed 's@__esct(\(.*)\)@$this->escape($this->translate(\1)@'  file.txt
share|improve this answer
Won't work, missing the final ) – Dennis Kaarsemaker Jan 17 '13 at 20:52
post edited accordingly =) – Gilles Quenot Jan 17 '13 at 20:54
It will now cause an extra ) to appear :) I think you mean (\(.*\)) like in my answer. – Dennis Kaarsemaker Jan 17 '13 at 20:56
2 opening (, 2 closing ) – Gilles Quenot Jan 17 '13 at 21:02
Thank you. worked like a charm! (can't upvote without 15 reputation apparently) – Ryan Eckert Jan 17 '13 at 21:12
sed -e 's/__esct(\(.*\))/$this->escape($this->translate(\1))/'

Should get you a long way. It'll fail if you have foo(__esct('Some text'))

sed -e 's/__esct(\([^(]*\))/$this->escape($this->translate(\1))/'

This one will work for those, but fail for __esct(foo('Some text'))

share|improve this answer
did you forget the final forward slash? – Ryan Eckert Jan 17 '13 at 21:06
Yup, edited to fix. – Dennis Kaarsemaker Jan 17 '13 at 21:17
thank you for your answer. I can only select one though and sputnick was itty-bitty faster:) – Ryan Eckert Jan 17 '13 at 21:26
No worries, use whatever works :) – Dennis Kaarsemaker Jan 17 '13 at 21:27

Perl has nice extensions to do something like that:

perl -pi -e 's;__esct\(\'([a-zA-Z ]*)\'\);\$this->escape\(\$this->translate\(\'\1\'));g'  list of the files to mangle

It will leave the originals as <file>.bak. This is just a global replace on each line, just as you'd write it in vi(1). In Perl, () and such are metacharacters, to match them you need \( \).

[I'm not 100% sure of the handling of the ', need to check what your particular shell does in such a case.]

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.