Issue
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.
Can we do the same in TCL also?
Solution
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 wordPRWD_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
The same effect can be accomplished with sed
or even grep
:
sed 's/.* PRWD_V .*; *//' file1
grep -oP '(.* PRWD_V .*; *)?\K.*' file1
Answered By - Ruud Helderman Answer Checked By - Pedro (WPSolving Volunteer)