Perl:仅在与特定正则表达式匹配的行上在引号之间打印字符串
Perl: print a string between quotes only on lines that match a certain regexp
编辑:我不只是想征求人们为我编写这段代码.....我已经尝试了大约一天,尽管有一本 perl 教科书,但我还是想不通。就像我说的,我可以使用 grep/awk/sed 来完成它,也可以通过将多个 perl 单行代码连接在一起....我只是想不出如何在一个 perl 调用中完成它。
我是 Perl 的新手,所以这个问题会很容易回答。
我正在尝试编写一个脚本来逐行解析文件并在引号之间的行中打印第一个单词,但是 仅 在 EOL 处包含特定字符串的行(字符串作为参数传递)。 C 头文件中的文件,连同其中的许多其他垃圾,包含以下格式的结构定义,其中包含我要提取的字符串。
struct Foo_t foo
{
/* str_HELLO */
{ {5,4,8,7,9},
{ "HELLO!", // English
"BONJOUR!", // French
"Hallo!", // German
"BONJOURNO!", // Italian
"HOLA!" // Spanish
} },
/* str_GOODBYE */
{ {15,3,3,3,3},
{ "GOODBYE!", // English
"AU REVOUIR!", // French
"TSCHUSS!", // German
"CIAO!", // Italian
"ADIOS!" // Spanish
} },
/* str_FOO */
{ {15,3,3,3,3},
{ "FOO", // English
"BAR", // French
"NOO", // German
"BAZ", // Italian
"OOF" // Spanish
} },
// lots more of these....
// ....
为了获得所需的输出,我希望调用是
bash~$: myscript.pl -language=english file_to_be_parsed.h
我可以通过管道将 greps 轻松地做到这一点,但我真的想在 perl 中得到它。我尝试通过脚本和单行代码来完成这个简单的任务,但是 none 成功了。由于这是如此之快,如果任何 perl 向导可以向我展示灯光,以及对匹配内容的解释,将不胜感激。
提前致谢!!
@melomene 在评论中的回答起到了作用。
我从
perl -wnle '/english/i and ($_ =~ /\"(\w)*\"/ and print $&);' file.h | perl -wlne 's/[",]//g and print;'
至
perl -wnle '/english/i and ($_ =~ /\"(\w*)\"/ and print );` file.h
类似的东西是对您自己的解决方案的改进。它应该被称为
perl filter_header.pl english header.h
use strict;
use warnings;
open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};
while ( <$fh> ) {
next unless m| // \s* \Q$lang |ix;
print , "\n" if / " ([^"]*) " /x;
}
输出
HELLO!
GOODBYE!
FOO
v5.22 的一个实验性功能是 code evaluation expressions
。如果成功,您可以从正则表达式中执行代码。结合积极的前瞻性,您会得到:
/"(.*?)".*(?=english)(?{print "\n";})$/i;
编辑:我不只是想征求人们为我编写这段代码.....我已经尝试了大约一天,尽管有一本 perl 教科书,但我还是想不通。就像我说的,我可以使用 grep/awk/sed 来完成它,也可以通过将多个 perl 单行代码连接在一起....我只是想不出如何在一个 perl 调用中完成它。
我是 Perl 的新手,所以这个问题会很容易回答。
我正在尝试编写一个脚本来逐行解析文件并在引号之间的行中打印第一个单词,但是 仅 在 EOL 处包含特定字符串的行(字符串作为参数传递)。 C 头文件中的文件,连同其中的许多其他垃圾,包含以下格式的结构定义,其中包含我要提取的字符串。
struct Foo_t foo
{
/* str_HELLO */
{ {5,4,8,7,9},
{ "HELLO!", // English
"BONJOUR!", // French
"Hallo!", // German
"BONJOURNO!", // Italian
"HOLA!" // Spanish
} },
/* str_GOODBYE */
{ {15,3,3,3,3},
{ "GOODBYE!", // English
"AU REVOUIR!", // French
"TSCHUSS!", // German
"CIAO!", // Italian
"ADIOS!" // Spanish
} },
/* str_FOO */
{ {15,3,3,3,3},
{ "FOO", // English
"BAR", // French
"NOO", // German
"BAZ", // Italian
"OOF" // Spanish
} },
// lots more of these....
// ....
为了获得所需的输出,我希望调用是
bash~$: myscript.pl -language=english file_to_be_parsed.h
我可以通过管道将 greps 轻松地做到这一点,但我真的想在 perl 中得到它。我尝试通过脚本和单行代码来完成这个简单的任务,但是 none 成功了。由于这是如此之快,如果任何 perl 向导可以向我展示灯光,以及对匹配内容的解释,将不胜感激。
提前致谢!!
@melomene 在评论中的回答起到了作用。
我从
perl -wnle '/english/i and ($_ =~ /\"(\w)*\"/ and print $&);' file.h | perl -wlne 's/[",]//g and print;'
至
perl -wnle '/english/i and ($_ =~ /\"(\w*)\"/ and print );` file.h
类似的东西是对您自己的解决方案的改进。它应该被称为
perl filter_header.pl english header.h
use strict;
use warnings;
open my $fh, '<', $file or die qq{Unable to open "$file" for input: $!};
while ( <$fh> ) {
next unless m| // \s* \Q$lang |ix;
print , "\n" if / " ([^"]*) " /x;
}
输出
HELLO!
GOODBYE!
FOO
v5.22 的一个实验性功能是 code evaluation expressions
。如果成功,您可以从正则表达式中执行代码。结合积极的前瞻性,您会得到:
/"(.*?)".*(?=english)(?{print "\n";})$/i;