Find a word and increment the number in the word & save into new files

Hi All,

I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl:

run_build_model sparc_ifu_dec
run_drc
set_faults -model path_delay -atpg_effectiveness -fault_coverage
add_delay_paths P1
set_atpg -abort_limit 1000
run_atpg -ndetects 1000

I would like to change P1 from P2 to P500 and save into new files such as P2.tcl,P3.tcl.... P500.tcl

I searched this on the web, but found only way to replace/generate one file. such as P2.tcl

Best,

Jaeyoung

try:

#!/bin/bash

for i in {2..500}
do
  awk '$2=="P1" {$2="P" i} 1' i=$i P1.tcl > "P$i.tcl"
done

Thank you so much.

It works, but there is a problem.

P1 is used in two places in my P1.tcl file.

  1. After add_delay_paths, P1: This changed correctly from P2 to P500.
  2. After write_patterns, P1.stil_in: This does not change. Still remain P1.

Could you let me how to change P1.stil_in from P2.stil_in to P500.stil_in?

read_netlist /home/unga/jp39467/Research/tmax/ccx/saed32nm_lvt.v -library

read_netlist /home/local/indus/jp39467/design/sys/iop/common/rtl/swrvr_clib.v -library
read_netlist /home/local/indus/jp39467/design/sys/iop/sparc/ifu/rtl/sparc_ifu_dec.v -library
read_netlist ./sparc_ifu_dec_flat.v
run_build_model sparc_ifu_dec
run_drc
set_faults -model path_delay -atpg_effectiveness -fault_coverage
add_delay_paths P1
set_atpg -abort_limit 1000
run_atpg -ndetects 1000
report_patterns -all
write_patterns P1.stil_in -internal -format stil -nocompaction -nopatinfo -parallel 0 -nocore
exit

I don't know the full scope of your problem, but maybe this approach fits: Have one specific file which holds the "highest number used so far". When you need to create a new sequence number, just increment (the content of) this file and use the new number. The only thing to take care of - but this applies to each possible solution - is concurrency: If two processes try to do the same, you have a race condition. Therefore you should lock the file, while using it.

awk only approach:

awk '
                {T[NR] = $0
                 if (/P1/) L[NR] = 1
                }

END             {for (j=2; j<=CNT; j++)
                        for (i=1; i<=NR; i++)   {if (L) sub ("P" j-1, "P" j, T)
                                                 print T > ("P" j ".tcl")
                                                }
                }
' CNT=12 file

With the loop in bash, and the contents modification done by sed

for i in {2..12}; do sed "s/\<P1\>/P$i/g" file > P"$i".tcl; done

Note there might be some old sed versions that do not understand the "left boundary" \< and "right boundary" \>

Thank you for all your helps.

I think RudiC's code works for me. Thanks again.

Best,

Jaeyoung