Home > Back-end >  find pattern print 2nd field and if not pattern print line without change using awk
find pattern print 2nd field and if not pattern print line without change using awk

Time:12-15

I have file in below format

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 

If line has PRWD_V pattern, I want to print $2 for that line and if pattern is not found in the line dont do anything print the line as it is. The output as below:

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 

I tried using awk

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

This is doing only for some lines and output is not coming properly.

CodePudding user response:

Assuming PRWD_V always occurs in the first part of the line, as is the case in the sample input, then the solution could be as simple as:

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

Explanation:

  • /.* PRWD_V .*; */ matches the first part of the line (including semicolon and surrounding spaces) if and only if that part contains the word PRWD_V.
  • sub(/.../,"") removes the matched part from the line (if and only if there was a match).
  • 1 prints whatever is left of the line.

Output:

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

CodePudding user response:

Does this solve your problem?

awk 'BEGIN{FS=OFS=";"} /PRWD_V/ {gsub("^ ", "", $2); print $2}; !/PRWD_V/ {print $0}' file1

CodePudding user response:

Using 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

CodePudding user response:

$ 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
  • Related