哪个命令可以快速搜索一行中的连续模式

时间:2016-07-21 11:13:22

标签: linux unix awk sed grep

哪个命令可以快速搜索unix中一行中的连续模式? “=”一词跟在“模型”之后。

输入文件

Model = It supports 10 Modular Controllers
Support Config Page Model = Yes
Model files are here

输出:

Extract the line where word "=" comes after the word "Model" and "Model" appears as a first word. 
Here first line of input file satisfy the criteria- "Model = It supports 10 Modular Controllers"

我使用过sed和awk命令,但想知道哪一个更好。

sed -n '/^Model/ s/=/&/p'
sed -n 's/^Model.*=/&/p'
sed -n '/^Model/ {/=/p ;}'

awk '/^Model.*=/'

有人可以告诉我哪一个更快更好。

1 个答案:

答案 0 :(得分:0)

正如Ed Morton所说,没有RegEx更快。我建议的解决方案是

awk '{a=index($0, "Model");b=index($0, "=")}a==1 && a<b'

首先我获得子串的位置,然后我比较它们以避免对Model进行双重搜索,另一种解决方案可能是:

awk 'index($0, "Model")!=1{next}index($0, "=")>1'

在这两个脚本中,我假设Model必须是第一个单词(你在regexp中使用“^”)在第二个脚本中我检查Model是否存在并且是字符串中的第一个单词,一旦这个验证我只检查“=”后面的“模型”(它的位置大于1)

相关问题