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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a word and increment the number in the word & save into new files
# 1  
Old 03-21-2016
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:

Code:
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
# 2  
Old 03-21-2016
try:
Code:
#!/bin/bash

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

# 3  
Old 03-21-2016
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?

Code:
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

# 4  
Old 03-22-2016
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.
# 5  
Old 03-22-2016
awk only approach:
Code:
awk '
                {T[NR] = $0
                 if (/P1/) L[NR] = 1
                }

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

# 6  
Old 03-22-2016
With the loop in bash, and the contents modification done by sed
Code:
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" \>
# 7  
Old 03-22-2016
Thank you for all your helps.

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

Best,

Jaeyoung
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find the number of occurence of particular word from a text file?

example: i have the following text file... i am very tired. i am busy i am hungry i have to find the number of occurence of a particular word 'am' from the text file.. can any one give the shell script for it (34 Replies)
Discussion started by: sheela
34 Replies

2. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

3. Shell Programming and Scripting

Find number of digits in a word

HI, Can you tell me how to find the number of digits in a word. $cat data.txt +123456ad 87645768 Output should be 6 8 (5 Replies)
Discussion started by: ashwin3086
5 Replies

4. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

5. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

6. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

7. UNIX for Dummies Questions & Answers

Script to search for a particular word in files and print the word and path name

Hi, i am new to unix shell scripting and i need a script which would search for a particular word in all the files present in a directory. The output should have the word and file path name. For example: "word" "path name". Thanks for the reply in adv,:) (3 Replies)
Discussion started by: virtual_45
3 Replies

8. Shell Programming and Scripting

To read data word by word from given file & storing in variables

File having data in following format : file name : file.txt -------------------- 111111;name1 222222;name2 333333;name3 I want to read this file so that I can split these into two paramaters i.e. 111111 & name1 into two different variables(say value1 & value2). i.e val1=11111 &... (2 Replies)
Discussion started by: sjoshi98
2 Replies

9. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies

10. Shell Programming and Scripting

TO find the word which occurs maximum number of times

Hi Folks !!!!!!!!!!!!!!!!!!! My Requirement is............. i have a input file: 501,501.chan 502,502.anand 503,503.biji 504,504.raja 505,505.chan 506,506.anand 507,507.chan and my o/p should be chan->3 i.e. the word which occurs maximum number of times in a file should be... (5 Replies)
Discussion started by: aajan
5 Replies
Login or Register to Ask a Question