d-_-b
d-_-b

Reputation: 6773

Regex / awk / etc for scanning my source code for translations

I'd like to scan my source for certain lines such as:

$obj->setLabel('output this text')->someOtherMethod(etc);

or:

$this->view->title = "I want this text";

Obviously the code is PHP. I'm using Zend Framework. That doesn't really matter.

I'm running linux and understand pipes. I'm guessing I could pipe:

grep --include=*.php -R 'setLabel(' .

into awk or whatever. I just want the each of the "one or more" characters output on it's own line surrounded by quotes, terminated by a comma. CSV translation files are not far off then.

I only expect to search for one pattern at a time. So first I'd get all the "Labels" etc.

note: I know of POedit, etc. I'm using CSV files for static UI translations. I'm not going to change that. They need to be editable by a 3rd party who just wants to use "Excel" (shudder...)

This is what I ended up using:

grep -oh --include=*.php -R -E "setLabel\('[^']*'\)" . > labels.txt

And then removing the unwanted "setLabel(" and ")" in a text editor. However, I'm very keen for a cleaner one-liner. Oh... There is also code-golf. I should ask those guys...

Upvotes: 2

Views: 220

Answers (2)

raphink
raphink

Reputation: 3665

How about using find and sed:

find . -type f -name '*.php' -exec sed -ne "s/.*setLabel('\([^']\+\)').*/\1/p" {} \;

and

find . -type f -name '*.php' -exec sed -ne "s/.*view->title = \"\([^\"]\+\)\".*/\1/p" {} \;

Upvotes: 2

kurumi
kurumi

Reputation: 25599

Ruby(1.9+)

say you want to search for setLabel

$ ruby -ne 'puts $_.scan(/.*setLabel\(\047(.[^)]*)\047/)' file
output this text

say you want to search for view-title

$ ruby -ne 'puts $_.scan(/.*view->title\s+=\s+\042(.[^"]*)\042/)' file
I want this text

Upvotes: 2

Related Questions