1

I have one file which contains several lines of data

editPin -pin phy_inst/i_dfi_row_cmd_p1_d[0] ctrl_soft_phy_inst/hbm_ch_tile_4_hbm_tile_inst/o_phy_row_cmd_p1[0] -assign {1443.0305 184.62} -layer M10 -pinWidth 0.038 -pinDepth 0.395 -fixOverlap false -fixedPin -snap MGRID
editPin -pin phy_inst/i_dfi_row_cmd_p1_d[1] ctrl_soft_phy_inst/hbm_ch_tile_4_hbm_tile_inst/o_phy_row_cmd_p1[0] -assign {1444.0305 185.62} -layer M10 -pinWidth 0.038 -pinDepth 0.395 -fixOverlap false -fixedPin -snap MGRID

and i want to perform some operation and i want to set my line like this

eval editPin -pin i_dfi_row_cmd_p1_d[0]  -assign { 0 [ expr 1443.0305 184.62]} -layer M10 -pinWidth 0.038 -pinDepth 0.395 -fixOverlap false -fixedPin -snap MGRID
eval editPin -pin i_dfi_row_cmd_p1_d[1]  -assign { 0 [ expr 1444.0305 185.62]} -layer M10 -pinWidth 0.038 -pinDepth 0.395 -fixOverlap false -fixedPin -snap MGRID

I used these commands individually, I tried them, and it worked how I wanted the structure.

awk '{$4=""; print $0}' all_pin 
awk '{print "eval " $0}' all_pin 
sed -e /^editPin/'{ s#phy_inst/## ; s/{/{ 0 [ expr / ; s/}/]}/ ; }' all_pin

but when i used these commands together its not giving the result how i want

cat all_pin | awk '{$4=""; print $0}' all_pin | awk '{print "eval " $0}' all_pin | sed -e /^editPin/'{ s#phy_inst/## ; s/{/{ 0 [ expr / ; s/}/]}/ ; }' all_pin

may i know the solution for this

and i want to perform addition operation for this block

-assign { 0 [ expr 1443.0305 362.764+X]}

X is a constant value and i want to add X to 362.764 using command.I tried but I got the wrong answer

awk '{print $10+100}' all_pin

please help me to get out of this

1
  • For your second part question, would you be happy with -assign { 0 [ expr 1443.0305 184.62 ]} instead of -assign { 0 [ expr 1443.0305 184.62]}? (There's an extra space between the second fractional number and the closing [}.) Oct 19, 2022 at 14:23

3 Answers 3

0

In your combined command you are still specifying a source file for each item in the pipeline:

cat all_pin | awk '{$4=""; print $0}' all_pin | awk '{print "eval " $0}' all_pin | sed -e /^editPin/'{ s#phy_inst/## ; s/{/{ 0 [ expr / ; s/}/]}/ ; }' all_pin
#   ^source                           ^again                             ^and again                                                                    ^and still again

Instead, you should let each command read from its predecessor. Here, I've combined the two awk commands and discarded the unnecessary cat:

awk '{ $4=""; print "eval " $0}' all_pin |
    sed -e '/^editPin/ { s#phy_inst/## ; s/{/{ 0 [ expr / ; s/}/]}/ ; }'

Depending on the answer to my comment question the second part may be equally straightforward, and I'll update my answer accordingly

1
  • As you mentioned here I already tried this, before posting a question, and also now I have tried, if I mention the filename "all_pin" for awk it will work (your solution) and if I mention the file name on both commands it will work only for sed command. Oct 20, 2022 at 9:59
0

Why not use tcl for the addition? Here is an example that just changes the expression to a have a nested expr so you can adjust the second value in tcl.

$ echo editPin -pin phy_inst/blah args  -assign {1443.0305 184.62} -options | \
    sed  '/editPin.*phy_inst/s/assign {\([0-9.]*\) \([0-9.]*\)}/assign { 0 [ expr \1 [ expr \2 + 123.3]]}/'
# returns editPin -pin phy_inst/blah args -assign { 0 [ expr 1443.0305 [ expr 184.62 + 123.3]]} -options

That way the sed is just a simple string replacement, and you aren't attempting to do any floating point math in shell/awk/bc/etc.

0

I came up with this awk

 {  for(i=1; i<=NF ; i++ ) {
        if ( $i ~ /^phy_inst/ ) {
                $i=substr($i,10) ;
                $(i+1)="" ; }
        if ( $i == "-assign" ) {
                $(i+1)="{ 0 [ expr " $(i+1) ;
                $(i+2)=$(i+2) "+" X " ]}" ;
                }
 }
        $1 = "eval " $1 ;
         print ;
 }

to be called with (replace foo_bar with proper value)

awk -v X=foo_bar -f SE.awk all_pin

yielding

eval editPin -pin i_dfi_row_cmd_p1_d[0]  -assign { 0 [ expr {1443.0305 184.62}+foo_bar ]} -layer M10 -pinWidth 0.038 -pinDepth 0.395 -fixOverlap false -fixedPin -snap MGRID
eval editPin -pin i_dfi_row_cmd_p1_d[1]  -assign { 0 [ expr {1444.0305 185.62}+foo_bar ]} -layer M10 -pinWidth 0.038 -pinDepth 0.395 -fixOverlap false -fixedPin -snap MGRID

where awk basically parse line by line,

  • parsing is done by looping on all "field", awk use NF variable (Number of Field, see man awk)
  • add eval in first field
  • strip phy_inst/ string, delete following field (implicit specification)
  • edit values after -assign field
  • insert value of X variable (this must be called on command line)(and a poor choice for a variable name)

Untested

    if ( $i == "-assign" ) {
            $(i+1)="{ 0 [ expr " ($(i+1)+X) ;
            $(i+2)=($(i+2) + X) " ]}" ;
            }

Just add X value to proper field.

3
  • That's great but i want to perform some arithmetic operation inside this "{ 0 [ expr {1443.0305 184.62}+foo_bar ]} " ,,,, its like " { 0 [ expr { 1443.0305 184.62+foo_bar } ]} " if foo_bar =1000 then the result will be " { 0 [ expr { 1443.0305 1846.2} ]} " Oct 20, 2022 at 10:29
  • Thanks, for the update. i got it i was working on that Oct 20, 2022 at 12:11
  • what is NF? in the for loop Oct 22, 2022 at 4:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .