如何 grep 查找后跟数字的模式并在数字大于 x 时打印到文件,

如何 grep 查找后跟数字的模式并在数字大于 x 时打印到文件,

我有一个如下所示的文件:

Thispeculiarpattern(1.00);thatpeculiarpattern(0.90);....
Thispeculiarpattern(0.73);thatpeculiarpattern(0.15);...................

Somerandomtext(0.81); somemorerandomtext(0.79):.................................
Somerandomtext(0.62); somemorerandomtext(0.04):..............
Herewegoagain(0.93);Herewegoyetagain(0.48);....
Herewegoagain(0.71);Herewegoyetagain(0.87);....

我想要这样的输出:

Thispeculiarpattern(1.00);thatpeculiarpattern(0.90);....
Somerandomtext(0.81); somemorerandomtext(0.79):....
Herewegoagain(0.71);Herewegoyetagain(0.87);....

即,我希望所有行输出到文件,如果它们包含“Thispeculiarpattern”、“Somerandomtext”或“Herewegoyetagain”,则后面必须跟一个值为 0.8 或以上的括号。

答案1

假设所有数字都是定点十进制(如示例所示):

使用 grep

$ grep -E '(Thispeculiarpattern|Somerandomtext|Herewegoyetagain)\(([1-9]|0\.[89])' file
Thispeculiarpattern(1.00);thatpeculiarpattern(0.90);....
Somerandomtext(0.81); somemorerandomtext(0.79):.................................
Herewegoagain(0.71);Herewegoyetagain(0.87);....

使用 sed

$ sed -En '/(Thispeculiarpattern|Somerandomtext|Herewegoyetagain)\(([1-9]|0\.[89])/p' file
Thispeculiarpattern(1.00);thatpeculiarpattern(0.90);....
Somerandomtext(0.81); somemorerandomtext(0.79):.................................
Herewegoagain(0.71);Herewegoyetagain(0.87);....

使用 awk

$ awk '/(Thispeculiarpattern|Somerandomtext|Herewegoyetagain)\(([1-9]|0\.[89])/' file
Thispeculiarpattern(1.00);thatpeculiarpattern(0.90);....
Somerandomtext(0.81); somemorerandomtext(0.79):.................................
Herewegoagain(0.71);Herewegoyetagain(0.87);....

怎么运行的

在所有情况下,我们都会查找与正则表达式匹配的行

(Thispeculiarpattern|Somerandomtext|Herewegoyetagain)\(([1-9]|0\.[89])

该正则表达式分为两部分。第一个是:

(Thispeculiarpattern|Somerandomtext|Herewegoyetagain)\(

上面的内容匹配您的三个字符串中的任何一个,后跟(.

第二部分是:

([1-9]|0\.[89])

这匹配以数字 1 到 9 开头的任何数字或者0.8以or开头的数字0.9

相关内容