I have two big files. file1 has data in below format
place u_RX_WR_SE1_LS { } R0 -place
place u_bank_LS { } R0 -place
file2 has data in below format
set inst RX_WR_SE1_PD ; add -inst $inst -cell PR ; place $inst 5.76 5305.84 0
set inst RX_WR_SE2_PD ; add -inst $inst -cell PR ; place $inst 5.76 5338.864 0
set inst bank_PD ; add -inst $inst -cell PR ; place $inst 5.76 5371.888 0
I want to search 2nd field removing starting u_
and _LS
from file1 i.e RX_WR_SE1
in file2, and if found in file2, for same match line copy field $13 $14
from file2 and add it inside { }
in file1.
The output should be in below format.
place u_RX_WR_SE1_LS { 5.76 5305.84 } R0 -place
place u_bank_LS { 5.76 5371.888 } R0 -place
Anyone who can help using TCL or SHELL? In shell I tried
awk 'FNR==NR{gsub(/u_|_LS/,"",$2);arr[$2]="";next}
{
for (i in arr) { if ($0 ~ i) {print i,$13,$14}}
}
' file1 file2
but this is not giving proper results .
In TCL it is also fine.
CodePudding user response:
I dont see any Trim or Matching conditions in your Code.
Try this,
`
awk 'FNR==NR{gsub(/u_|_LS/,"",$2);arr[$2]="";next}
{
for (i in arr) { if ($0 ~ i) {print i,$13,$14}}
}
' file1 file2
`
RX_WR_SE1 5.76 5305.84
bank 5.76 5371.888
gsub performs the Trim for your text, so as to match with the next file for loop performs the matching.
It's not fully up to your expectations of the output, but I believe it provides a base for you.
CodePudding user response:
With Tcl
# extract the data from file2
set fh [open file2]
set values {}
while {[gets $fh line] != -1} {
set fields [regexp -all -inline {\S } $line]
set inst_name [regsub -- {_PD$} [lindex $fields 2] ""]
dict set values $inst_name [lrange $fields 12 13]
}
close $fh
# read file1, replace the values, and store in a new file
set fin [open file1]
set fout [open file1.new w]
while {[gets $fin line] != -1} {
set inst_name [regsub {^u_(. )_LS$} [lindex [split $line] 1] {\1}]
puts $fout [format {place %s {%s} R0 -place} $inst_name [dict get $values $inst_name]]
}
close $fin
close $fout
# move the new contents to file1
# remove next line if you don't need to backup the file
file link -hard file1 file1.bak
file rename -force file1.new file1