shell 查找模式打印第2个字段,如果不是,则使用awk不更改模式打印行

deikduxw  于 2022-12-19  发布在  Shell
关注(0)|答案(3)|浏览(150)

我有以下格式的文件

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

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

0lvr5msh

0lvr5msh1#

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

bqujaahr2#

假设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也可以实现相同的效果:
一个二个一个一个

nr7wwzry

nr7wwzry3#

使用sed

$ sed sed 's/.*PRWD_V[^;]*;[ \t]*//' 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

相关问题