Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于扫描源代码进行翻译的Regex/awk/etc_Regex_Unix_Grep_Translate - Fatal编程技术网

用于扫描源代码进行翻译的Regex/awk/etc

用于扫描源代码进行翻译的Regex/awk/etc,regex,unix,grep,translate,Regex,Unix,Grep,Translate,我想扫描源代码中的某些行,例如: $obj->setLabel('output this text')->someOtherMethod(etc); 或: 显然,代码是PHP。我正在使用Zend框架。这并不重要 我正在运行linux并了解管道。我猜我可以用烟斗: grep --include=*.php -R 'setLabel(' . 进入awk或其他什么。我只想在自己的行中输出“一个或多个”字符,并用引号括起来,以逗号结尾。CSV翻译文件就在眼前 我只希望一次搜索一种模式。所

我想扫描源代码中的某些行,例如:

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

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

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

grep --include=*.php -R 'setLabel(' .
进入awk或其他什么。我只想在自己的行中输出“一个或多个”字符,并用引号括起来,以逗号结尾。CSV翻译文件就在眼前

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

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

这就是我最终使用的:

grep -oh --include=*.php -R -E "setLabel\('[^']*'\)" . > labels.txt
然后在文本编辑器中删除不需要的“setLabel(“And”)”。然而,我非常渴望一个更干净的一班轮。哦还有高尔夫代码。我应该问问那些家伙…

Ruby(1.9+)

假设您要搜索
setLabel

$ ruby -ne 'puts $_.scan(/.*setLabel\(\047(.[^)]*)\047/)' file
output this text
假设您要搜索
查看标题

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

使用
find
sed
如何:

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


很有意思,但很抱歉,我不会仅仅为了这个而安装Ruby。它确实需要基本的GNU/Linux CLI功能。我现在得到了这个:grep-oh-include=*.php-R-e“setLabel('.'.')”。现在这个:grep-oh-include=*.php-R-e“setLabel('[^]*')”天才!谢谢sed是否具有与grep相同的正则表达式功能?大括号和斜杠的作用是什么?是的,
sed
使用标准的基本POSIX正则表达式,如
grep
。大括号将替换为与
find
表达式匹配的文件名,以便将这些文件传递到
sed
\
用于标记传递给
find
表达式中的
-exec
的命令的结束。在最新版本的
find
上,您也可以使用
\+
而不是
\
一次将所有文件传递到
sed
,而不是逐个传递每个文件。如果命令(如
sed
)支持将多个文件作为输入,这通常会更快。非常有趣。感谢您的后续解释。
awk也允许模式匹配。一般经验法则:如果你想使用
grep
,然后将其导入
sed
awk
,那么就放弃
grep
,因为你是在白白浪费资源。是的,在95%的情况下也放弃
cat
。-)
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" {} \;