Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

3 Answers 3

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 =) –  sputnick 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 ) –  sputnick 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

 
discard

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.