我编写了用于解析输入文本/文件的perl脚本如下:
[root@agent tmp]# head ./test_regqexp_keynote.txt
alert@keynote.com (Top20) Carnation GB/fd (Crit) 9:57 11 KB
alert@keynote.com (Shell Brand EUR) HealthScience NL/fd (Crit) 9:36
11 KB
alert@keynote.com (Shell Corp AMS) Nestle JP/fd (Crit) 9:16 11 KB
alert@keynote.com (Top20) NestleHealth DE/fd (Crit) 9:01 11 KB
alert@keynote.com (Shell Corp AMS) Nestle NZ/fd (Crit) 8:17 12 KB
alert@keynote.com (Shell Brand EUR) HealthScience CH/fd (Crit) 8:11
11 KB
alert@keynote.com (Shell Corp AMS) Nestle CL/fd (Crit) 8:11 11 KB
alert@keynote.com (Shell Corp AMS) Nestle VE/fd (Crit) 8:11 11 KB并在输出中获得如下文本:
HealthScience FI/fd
DolceGusto NZ/fd
Waters WW/fd
Nestle UA/fd
Nestle SK/fd
Nestle SI/fd
perl -ne 'if ($_=~ m/([a-zA-Z]* [A-Z]{2,3}\/fd)/) {print "$1\n";}'因此,只摘录"XXXX /fd“部分。
但是我需要使用SED工具来完成它。
这让我发疯了。似乎SED以完全不同的方式工作,或者有非常不同的RegEx规则。
请帮助我将此RegExp查询转换为SED RegExp查询。或者解释Sed RegExp与Perl或Egrep RegExp的主要不同之处。
发布于 2014-05-30 08:33:35
sed -n 's#.* \([a-zA-Z]* [A-Z]\{2,3\}/fd\).*#\1#p' test_regqexp_keynote.txt或者:
egrep -o '[a-zA-Z]* [A-Z]{2,3}/fd' test_regqexp_keynote.txt发布于 2014-05-30 08:29:13
有一种方法:
sed -n 's!.* \([a-zA-Z]* [A-Z]\{2,3\}/fd\).*!\1!p' input也许这也能奏效:
sed -n 's!.* \([^ ]* [^ ]*/fd\).*!\1!p' input发布于 2014-05-30 08:42:34
您可以尝试这个awk,而不是复杂的正则表达式。
awk -F"[)] | [(]" 'NF>3{print $3}' file
Carnation GB/fd
HealthScience NL/fd
Nestle JP/fd
NestleHealth DE/fd
Nestle NZ/fd
HealthScience CH/fd
Nestle CL/fd
Nestle VE/fdhttps://stackoverflow.com/questions/23950040
复制相似问题