过滤命令的输出时,正则表达式错误 [英] regexp error when filtering command output
问题描述
我想将行从以下命令进行过滤:
I want to filter the lines from the following command:
$ cat /proc/net/route
Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT
br-lan 01020300 C0A80101 0003 0 0 3 FFFFFF00 0 0 0
br-lan 03043836 C0A80101 0007 0 0 5 FFFFFFFF 0 0 0
br-lan C0A80100 00000000 0001 0 0 0 FFFFFF00 0 0 0
我想只提取包含目标行01020300
和面具 FFFFFF00
我试着用下面的正则表达式,但它不工作
I tried with the following regexp but it does not works
cat /proc/net/route | grep "[^ \t\v]\+[ \t\v]\+$dest[ \t\v]\+[^ \t\v]\+[ \t\v]\+[^ \t\v]\+[ \t\v]\+[^ \t\v]\+[ \t\v]\+[^ \t\v]\+[ \t\v]\+[^ \t\v]\+[ \t\v]\+$mask"
我newbe wuth正则表达式的格式。你能不能帮我一个良好的正则表达式这种情况?
I newbe wuth the regexp format. could you help me with a good regexp for this case?
推荐答案
也许更容易与 AWK
来工作了这一点:
Maybe it is easier to work with awk
for this:
$ awk '$2=="01020300" && $8=="FFFFFF00"' file
br-lan 01020300 C0A80101 0003 0 0 3 FFFFFF00 0 0 0
这种方式可以参考第2和第8列,并检查它的价值,而不必担心格式。
This way you can refer to the 2nd and 8th columns and check its value, without having to worry about the format.
如果您的文件,制表符代替空格隔开fiels,使用方法:
In case your file separates fiels with tabs instead of spaces, use:
awk -F"\t" '$2=="01020300" && $8=="FFFFFF00"' file
而如果你想给从bash的变量值这个,你可以做(见注释,格伦·杰克曼,伊坦赖斯纳和埃德·莫顿给了很好的解释):
And in case you want to give this values from bash variables, you can do (see comments, as Glenn Jackman, Etan Reisner and Ed Morton gave nice explanations):
awk -v d="$dest" -v m="$mask" '$2==d && $8==m' file
这篇关于过滤命令的输出时,正则表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!