Grep a matched line includes below and another matched line includes above

I would like to print a matched line include one line below and another matched line include above.

Here is my file:

$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)

Output should be:

$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

I know grep is a tool for this.

grep -A 1 name file.txt  

This prints a matched line including below

grep -B 1 critical file.txt 

This prints another matched line including above.

But, I don't know how to combine two. Any help is appreciated.


With GNU grep:

grep -Poz --color=never '.*name.*n.*|.*n.*critical.*' file.txt

Output:

$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

From man grep :

-P : Interpret PATTERN as a Perl regular expression.

-o : Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

-z : Treat the input as a set of lines, each terminated by a zero byte (the ASCII NUL character) instead of a newline


I'm not sure that grep can handle your case, but AWK can:

awk '/name/{print;getline;print}/critical/{print prev;print}{prev=$0}' file.txt

Breakdown:

/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/19658.html

上一篇: 从grep中排除.svn目录

下一篇: grep匹配的行包括下面,另一个匹配的行包括上面