【问题标题】:find pattern print 2nd field and if not pattern print line without change using awk找到模式打印第二个字段,如果没有使用 awk 更改模式打印行
【发布时间】:2022-12-19 17:45:44
【问题描述】:

我有以下格式的文件

addInst -inst RESET_n_RS  -cell PRWD_V ; placeInstance RESET_n_RS  4653.84 10212 R180 
addInst -inst MODE_RS  -cell PRWD_V ; placeInstance MODE_RS  4686.864 10212 R180 
addInst -inst VDD_PD_T_4 -cell PVDD1_V  ;   placeInstance VDD_PD_T_4 4719.888 10212 R180 
addInst -inst VDD_IO_PD_T_5 -cell PVDD2_V ; placeInstance  VDD_IO_PD_T_5 4785.888 10212 R180 
placeInstance  DBG_RS 4861.728 10212 R180 
placeInstance  HAST_RS 4894.752 10212   R180 
addInst -inst VDD_PD_T_8  -cell PVDD1_V ;   placeInstance VDD_PD_T_8   4927.776 10212   R180 
placeInstance  WIRE_RS 4993.776 10212   R180 
addInst -inst EN0_RS  -cell PRWD_V ; placeInstance EN0_RS  5026.8 10212 R180 

如果行有PRWD_V 模式,我想为该行打印$2,如果在该行中找不到模式,则不要按原样打印该行。 输出如下:

placeInstance RESET_n_RS  4653.84 10212 R180 
placeInstance MODE_RS  4686.864 10212 R180 
addInst -inst VDD_PD_T_4 -cell PVDD1_V  ;   placeInstance VDD_PD_T_4 4719.888 10212 R180 
addInst -inst VDD_IO_PD_T_5 -cell PVDD2_V ; placeInstance  VDD_IO_PD_T_5 4785.888 10212 R180 
placeInstance  DBG_RS 4861.728 10212 R180 
placeInstance  HAST_RS 4894.752 10212   R180 
addInst -inst VDD_PD_T_8  -cell PVDD1_V ;   placeInstance VDD_PD_T_8   4927.776 10212   R180 
placeInstance  WIRE_RS 4993.776 10212   R180 
placeInstance EN0_RS  5026.8 10212 R180 

我尝试使用 awk

awk -F';' '{for (i=1;i<=NF;i++) if ($i ~ /PRWD_V/) print $2} {print $1,$2}' file1 

这仅适用于某些行,输出不正确。

我们也可以在TCL做同样的事情吗?

【问题讨论】:

    标签: shell awk sed tcl


    【解决方案1】:
    $ awk -F' *; *' '{print (/PRWD_V/ ? $2 : $0)}' file
    placeInstance RESET_n_RS  4653.84 10212 R180
    placeInstance MODE_RS  4686.864 10212 R180
    addInst -inst VDD_PD_T_4 -cell PVDD1_V  ;   placeInstance VDD_PD_T_4 4719.888 10212 R180
    addInst -inst VDD_IO_PD_T_5 -cell PVDD2_V ; placeInstance  VDD_IO_PD_T_5 4785.888 10212 R180
    placeInstance  DBG_RS 4861.728 10212 R180
    placeInstance  HAST_RS 4894.752 10212   R180
    addInst -inst VDD_PD_T_8  -cell PVDD1_V ;   placeInstance VDD_PD_T_8   4927.776 10212   R180
    placeInstance  WIRE_RS 4993.776 10212   R180
    placeInstance EN0_RS  5026.8 10212 R180
    

    【讨论】:

      【解决方案2】:

      假设 PRWD_V 总是出现在行的第一部分,就像示例输入中的情况一样,那么解决方案可以很简单:

      awk '{sub(/.* PRWD_V .*; */,"")}1' file1
      

      解释:

      • /.* PRWD_V .*; */ 匹配行的第一部分(包括分号和周围的空格)当且仅当该部分包含单词PRWD_V
      • sub(/.../,"") 从行中删除匹配的部分 (当且仅当有一场比赛)。
      • 1 打印该行剩余的内容。

      输出:

      placeInstance RESET_n_RS  4653.84 10212 R180
      placeInstance MODE_RS  4686.864 10212 R180
      addInst -inst VDD_PD_T_4 -cell PVDD1_V  ;   placeInstance VDD_PD_T_4 4719.888 10212 R180
      addInst -inst VDD_IO_PD_T_5 -cell PVDD2_V ; placeInstance  VDD_IO_PD_T_5 4785.888 10212 R180
      placeInstance  DBG_RS 4861.728 10212 R180
      placeInstance  HAST_RS 4894.752 10212   R180
      addInst -inst VDD_PD_T_8  -cell PVDD1_V ;   placeInstance VDD_PD_T_8   4927.776 10212   R180
      placeInstance  WIRE_RS 4993.776 10212   R180
      placeInstance EN0_RS  5026.8 10212 R180
      

      使用sed 甚至grep 也可以达到同样的效果:

      sed 's/.* PRWD_V .*; *//' file1
      
      grep -oP '(.* PRWD_V .*; *)?K.*' file1
      

      【讨论】:

      • 我们可以使用 TCL 做同样的事情吗?
      【解决方案3】:

      使用sed

      $ sed sed 's/.*PRWD_V[^;]*;[ 	]*//' input_file
      placeInstance RESET_n_RS  4653.84 10212 R180
      placeInstance MODE_RS  4686.864 10212 R180
      addInst -inst VDD_PD_T_4 -cell PVDD1_V  ;   placeInstance VDD_PD_T_4 4719.888 10212 R180
      addInst -inst VDD_IO_PD_T_5 -cell PVDD2_V ; placeInstance  VDD_IO_PD_T_5 4785.888 10212 R180
      placeInstance  DBG_RS 4861.728 10212 R180
      placeInstance  HAST_RS 4894.752 10212   R180
      addInst -inst VDD_PD_T_8  -cell PVDD1_V ;   placeInstance VDD_PD_T_8   4927.776 10212   R180
      placeInstance  WIRE_RS 4993.776 10212   R180
      placeInstance EN0_RS  5026.8 10212 R180
      

      【讨论】:

        猜你喜欢
        • 2014-07-10
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多