Jaeyoung Park
Jaeyoung Park

Reputation: 339

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

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

Upvotes: 0

Views: 99

Answers (2)

Sébastien Guarnay
Sébastien Guarnay

Reputation: 126

awk is less clear but faster for such long loops:

awk '{for (n=2; n<=500; n++) {line = gensub(/P1/, "P" n, "g", $0); print line > "P" n ".tcl"}}' P1.tcl

using gensub to replace P1 by "P" n.

Upvotes: 1

Brian
Brian

Reputation: 2242

Untested:

for i in {2..500}; do
    cp P1.tcl P$i.tcl
    sed -i s/P1/P$i/ P$i.tcl
done

Upvotes: 1

Related Questions