2

我想扫描我的源代码以查找某些行,例如:

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

或者:

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

显然代码是PHP。我正在使用 Zend 框架。这并不重要。

我正在运行 linux 并了解管道。我猜我可以管道:

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

进入 awk 或其他。我只希望在它自己的行上输出的每个“一个或多个”字符都用引号括起来,用逗号终止。CSV 翻译文件离那时不远了。

我只希望一次搜索一种模式。所以首先我会得到所有的“标签”等。

注意: 我知道 POedit 等。我正在使用 CSV 文件进行静态 UI 翻译。我不会改变这一点。它们需要由只想使用“Excel”的第 3 方编辑(颤抖……)

这就是我最终使用的:

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

然后在文本编辑器中删除不需要的“setLabel(”和“)”。但是,我非常渴望更清洁的单线。哦...还有代码高尔夫。我应该问问那些人...

4

2 回答 2

2

红宝石(1.9+)

说你要搜索setLabel

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

说你要搜索view-title

$ ruby -ne 'puts $_.scan(/.*view->title\s+=\s+\042(.[^"]*)\042/)' file
I want this text
于 2011-02-10T11:49:07.150 回答
2

如何使用findand sed

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

find . -type f -name '*.php' -exec sed -ne "s/.*view->title = \"\([^\"]\+\)\".*/\1/p" {} \;
于 2011-02-10T15:17:09.560 回答