2012-03-01 45 views
0

心中已經發現了用於本地化字符串,項目像這樣:獲取文本已經由正則表達式

{{ "I'd like this quote to be localized" | trans }} 
{{ "this_is_a_key" |trans}} 
{{'another text'| trans}} 
{{"ohter"|trans }} 
{{'no_spaces'|trans}} 

使用這個命令:

find src/ -type f -name '*.twig' -execdir egrep -o -- "\{\{[^|()}{]*\|[(trans) ^}]*\}\}" {} \;

我可以在我的各種模板文件中找到所有這些,女巫已經很棒了。

但是我想更進一步,直接獲取上面例子中的「」或「'之間的內容。如何自定義我的搜索或進行其他搜索後提取?

PS:我現在.SH腳本是:

old_IFS=$IFS 
IFS=$'\n' 
transFound = 0; 

allUsed=`find src/ -type f -name '*.twig' -execdir egrep -o -- "\{\{[^|(){]*\|[(trans) ^}]*\}\}" {} \;` 
defaultFile="messages.yml" 

for key in $allUsed 
    do 
     echo $key 
     let "transfound++" 
    done 

echo "Found $transfound translations" 
+0

也許我該用| sed'模式',但不知道如何.. – guillaumepotier 2012-03-01 11:57:19

回答

0

我終於找到它;)只好用SED:

find src/ -type f -name '*.twig' -execdir egrep -o -- "\{\{ ?['\"][^|()}{]*\|[(trans) ^}]*\}\}" {} \; | sed "s/.*'\(.*\)'[^']*$/\1/" | sed 's/.*"\(.*\)"[^"]*$/\1/' 
0

您可以使用perl-P)正則表達式,和only-o)打印出匹配

$ grep -oP "(['\"]).*\1" input.txt 
"I'd like this quote to be localized" 
"this_is_a_key" 
'another text' 
"ohter" 
'no_spaces' 

-o, --only-matching 
      Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line. 

    -P, --perl-regexp 
      Interpret PATTERN as a Perl regular expression (PCRE, see below). This is highly experimental and grep -P may warn of unimplemented 
      features.