3

我需要一个在 Windows 中运行的简单脚本,它在大型xml 文件中搜索关键字,然后返回它之前的单词、关键字和之后的单词。

例如:“我如何在上下文中提取关键字”我想要:“提取关键字

我是一个新手,有足够的知识来用关键字返回每一行,以及之前和之后的,但我很难得到我需要的单个单词。

任何人有任何聪明的想法?

4

2 回答 2

2

这是一种方法:

#!/usr/bin/perl

use 5.12.0;
my $keyword = 'keywords';

while (<DATA>)
{
    say for /\b(\S+\s+\b\Q$keyword\E[[:punct:]]*\s+\S+)\b/g;
}

__END__
How can I extract keywords in context, even if there are many keywords to
extract? So many keywords, no idea how to deal with them.
于 2011-10-06T17:30:18.183 回答
0

grep -o足够:

grep -Po '(\S+\s)?keywords(\s\S+)?' << END
How can I extract keywords in context
How can I extract keywords
keywords in context
END

返回

extract keywords in
extract keywords
keywords in
于 2011-10-06T17:55:24.593 回答