grep匹配的行包括下面,另一个匹配的行包括上面
我想打印一个匹配的行,其中包含一行,另一行匹配的行包括上面。
这是我的文件:
$name "path_sparc_ifu_dec_in_1" ;
dtu_inst_d[19] (in) 0.00 0.00 f
U1970/Y (INVX0_RVT) 0.03 0.03 r
U509/Y (AND2X1_RVT) 0.08 0.11 r
U1845/Y (NAND3X2_RVT) 0.13 0.24 f
U1866/Y (INVX0_RVT) 0.07 0.31 r
U482/Y (NAND2X0_RVT) 0.07 0.39 f
U1634/Y (NAND2X2_RVT) 0.13 0.51 r
U480/Y (AO222X1_RVT) 0.12 0.63 r
U1653/Y (AO22X2_RVT) 0.11 0.74 r
U1652/Y (DELLN1X2_RVT) 0.27 1.01 r
U363/Y (AND4X1_RVT) 0.11 1.12 r
ifu_exu_muldivop_d[3] (out) 0.00 1.12 r
critical voltage 1.00838
----------------------------------------------------------------------
(Path is unconstrained)
Startpoint: dtu_inst_d[21]
(input port)
Endpoint: ifu_exu_wen_d
(output port)
Path Group: (none)
Path Type: max
Point Incr Path
----------------------------------------------------------------------
$name "path_sparc_ifu_dec_in_2" ;
dtu_inst_d[21] (in) 0.00 0.00 r
U1985/Y (INVX0_RVT) 0.04 0.04 f
U1872/Y (AND2X1_RVT) 0.10 0.14 f
U1847/Y (IBUFFX2_RVT) 0.11 0.25 r
U1846/Y (INVX0_RVT) 0.06 0.31 f
U439/Y (NAND2X0_RVT) 0.09 0.39 r
U438/Y (NAND2X0_RVT) 0.09 0.48 f
U1119/Y (INVX1_RVT) 0.07 0.55 r
U1120/Y (INVX1_RVT) 0.04 0.59 f
U622/Y (INVX0_RVT) 0.05 0.64 r
U329/Y (NAND4X0_RVT) 0.06 0.70 f
U325/Y (AO221X1_RVT) 0.16 0.86 f
U908/Y (AO21X2_RVT) 0.12 0.99 f
U1856/Y (AO21X2_RVT) 0.12 1.11 f
ifu_exu_wen_d (out) 0.00 1.11 f
critical voltage 1.00275
----------------------------------------------------------------------
(Path is unconstrained)
Startpoint: dtu_inst_d[22]
(input port)
Endpoint: ifu_exu_wen_d
(output port)
Path Group: (none)
Path Type: max
Point Incr Path
----------------------------------------------------------------------
$name "path_sparc_ifu_dec_in_3" ;
dtu_inst_d[22] (in) 0.00 0.00 f
U584/Y (NBUFFX2_RVT) 0.06 0.06 f
U1872/Y (AND2X1_RVT) 0.08 0.14 f
U1847/Y (IBUFFX2_RVT) 0.11 0.25 r
U1846/Y (INVX0_RVT) 0.06 0.31 f
U439/Y (NAND2X0_RVT) 0.09 0.39 r
U438/Y (NAND2X0_RVT) 0.09 0.48 f
U1119/Y (INVX1_RVT) 0.07 0.55 r
U1120/Y (INVX1_RVT) 0.04 0.59 f
U622/Y (INVX0_RVT) 0.05 0.64 r
U329/Y (NAND4X0_RVT) 0.06 0.70 f
U325/Y (AO221X1_RVT) 0.16 0.86 f
U908/Y (AO21X2_RVT) 0.12 0.99 f
U1856/Y (AO21X2_RVT) 0.12 1.10 f
ifu_exu_wen_d (out) 0.00 1.10 f
critical voltage 1.00174
----------------------------------------------------------------------
(Path is unconstrained)
输出应该是:
$name "path_sparc_ifu_dec_in_1" ;
dtu_inst_d[19] (in) 0.00 0.00 f
ifu_exu_muldivop_d[3] (out) 0.00 1.12 r
critical voltage 1.00838
$name "path_sparc_ifu_dec_in_2" ;
dtu_inst_d[21] (in) 0.00 0.00 r
ifu_exu_wen_d (out) 0.00 1.11 f
critical voltage 1.00275
$name "path_sparc_ifu_dec_in_3" ;
dtu_inst_d[22] (in) 0.00 0.00 f
ifu_exu_wen_d (out) 0.00 1.10 f
critical voltage 1.00174
我知道grep是一个工具。
grep -A 1 name file.txt
这会打印包含下面的匹配行
grep -B 1 critical file.txt
这会打印另一条包含上面的匹配行。
但是,我不知道如何结合两个。 任何帮助表示赞赏。
使用GNU grep:
grep -Poz --color=never '.*name.*n.*|.*n.*critical.*' file.txt
输出:
$name "path_sparc_ifu_dec_in_1" ; dtu_inst_d[19] (in) 0.00 0.00 f ifu_exu_muldivop_d[3] (out) 0.00 1.12 r critical voltage 1.00838 $name "path_sparc_ifu_dec_in_2" ; dtu_inst_d[21] (in) 0.00 0.00 r ifu_exu_wen_d (out) 0.00 1.11 f critical voltage 1.00275 $name "path_sparc_ifu_dec_in_3" ; dtu_inst_d[22] (in) 0.00 0.00 f ifu_exu_wen_d (out) 0.00 1.10 f critical voltage 1.00174
从man grep
:
-P
:将PATTERN解释为Perl正则表达式。
-o
:仅打印匹配行的匹配(非空白)部分,每个这样的部分位于单独的输出行中。
-z
:将输入视为一组行,每行都以零字节(ASCII NUL字符)结尾,而不是换行符
我不确定grep
能处理你的情况,但AWK可以:
awk '/name/{print;getline;print}/critical/{print prev;print}{prev=$0}' file.txt
分解:
/name/ { # Match name
print; # Print current line
getline; # Get next line
print; # Print it
}
/critical/ { # Match critical
print prev; # Print previous line
print; # Print current line
}
{
prev = $0 # Store previous line in prev
}
awk
来拯救!
$ awk '/$name/{c=2} c&&c--; /critical/{print p; print} {p=$0}' file
$name "path_sparc_ifu_dec_in_1" ;
dtu_inst_d[19] (in) 0.00 0.00 f
ifu_exu_muldivop_d[3] (out) 0.00 1.12 r
critical voltage 1.00838
$name "path_sparc_ifu_dec_in_2" ;
dtu_inst_d[21] (in) 0.00 0.00 r
ifu_exu_wen_d (out) 0.00 1.11 f
critical voltage 1.00275
$name "path_sparc_ifu_dec_in_3" ;
dtu_inst_d[22] (in) 0.00 0.00 f
ifu_exu_wen_d (out) 0.00 1.10 f
critical voltage 1.00174
链接地址: http://www.djcxy.com/p/19657.html
上一篇: Grep a matched line includes below and another matched line includes above