我想打印一条匹配的行,包括下面一行,另一行包括上面的匹配行。
这是我的档案:
$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
这打印另一个匹配的行,包括上面的。
但是,我不知道如何将两者结合起来。任何帮助表示赞赏。
答案 0 :(得分:2)
我不确定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
}
答案 1 :(得分:1)
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
答案 2 :(得分:1)
使用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字符)而不是换行符结束
答案 3 :(得分:0)
$ awk 'f || /critical/{print prev ORS $0; f=0} /name/{f=1} {prev=$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