UNIX replacing and incrementing number

Hi

I am unix newbie looking for a unix bash script that can make it easier to do my code work. we have a code number for each code block that we want to incrementally assign. We have 10000 of these and it is very laborious to do this one by one.

so what we want is start from the top of the file , look for '0000' or text below whatver number is in it. start Update with 0001 and next wherever there is number like '0000' and also has '.001' int he same line then the number repeats. .001 does not change. Example below.

l_code_number := (l_src||'0011')::num

l_code_number := (l_src||'0012')::num

l_code_number := (l_src||'0012'||'.001')::num

l_code_number := (l_src||'0013')::num

l_code_number := (l_src||'0014')::num

l_code_number := (l_src||'0014'||'.001')::num
.
..
...

If the number is more 9999 thats ok. but till 9999 it should have pad of 4 zeros 0000. Please help. no perl please. So when ever I run the script to a file, the output should have the numbers replaced inside the file or to the output file.

We like to see some effort from our new members, before helping.

But since you are new, and this is your first post, here is one possible attempt :

awk 'match($3,/[0-9][0-9][0-9][0-9]/) { dgt=substr($3,RSTART,RLENGTH); if (dgt < 9999 ) { sub(dgt,sprintf ("%04d",dgt+1),$3) } } 1 ' inputfile 

This is GNU awk GNU Awk 4.2.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)
If you are satisfied with result, redirect it to output file.

Please, in future requests, provide some of your code / attempts first.

Hope that helps
Regards
Peasant.

I am afraid that from your description I have no idea what you are trying to do.

You show us sample input that has absolutely no occurrences of the string '0000' and you do not show us the output that you want to be produced from that input.

You say that changed numbers less than 9999 should have a pad of four zeroes, but none of your samples do that. They are padded to four digits, but none of them have four leading zeroes as requested.

Please clearly explain what you are trying to do, show us sample input that shows us the various things you are trying to do (in CODE tags), and show us the corresponding output that you hope to produce from that sample input.

If you have a bunch of input files, is each file supposed to modified to change '0000' to sequential values starting at one or are the numbers to be assigned sequentially such that the same number is never assigned in more than one output file?

Where are the files that are to be used as input files? If there are files in that directory that are not to be modified, how is your script supposed to know which files are to be processed? Where are the output files to be placed?

Hi Don/Peasant,

Peasant, I did not have a clue on how to start, had a a mental block so could not do an attempt. I am searching for a script that does atleast some part of it but whenever i change it i get errors and just left me frustrated. Hence i made this post. I will try this and see if it works. thank you for your attempt. I will add a post once i give it a shot.

Don,

Looks like I messed up. Sorry about missing code tags. I will put extra effort to follow the rules. I would recommend if you can put some validation so it does not allow any one posting without code tags. But you will not see that from me every again hopefully. I will put in the explanation with more information.

we have one file that has our sql code that has code numbers to identify if there is an error we can find it with the code number. those code number should should start with

'0001'

and have code section and then

 '0002' 

and so on, where ever there is a .

.001 

, ""the current number" repeats.

It looks like this. I have changed it start from 1. so Basically I want to update what ever is the number in the

'0001' 

. I have already built up the file by copying pasting code blocks but not updated the numbers 1 by 1. it is a 10000 code blocks file. and it is very laborious to change it one by one to 10000

l_code_number := (l_src||'0001')::num
(code)
l_code_number := (l_src||'0002')::num
(code)
l_code_number := (l_src||'0002'||'.001')::num
(code)
l_code_number := (l_src||'0003')::num
(code)
l_code_number := (l_src||'0004')::num
(code)
l_code_number := (l_src||'0004'||'.001')::num

hmmm... not sure... I'll give it a whirl:

awk -F\' '/l_code_number.*:: *num *$/ {$2=($4==.001) ? last_field2 : ++start; $2=sprintf("%04d", $2); last_field2=$2;} 1 ' OFS=\' input_file