UNIX replacing and incrementing number


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers UNIX replacing and incrementing number
# 1  
Old 08-03-2019
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.

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

Last edited by Don Cragun; 08-03-2019 at 02:16 AM.. Reason: Add CODE and ICODE tags.
# 2  
Old 08-03-2019
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 :
Code:
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.
# 3  
Old 08-03-2019
Quote:
Originally Posted by chamajid
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.

Code:
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.
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?
# 4  
Old 08-03-2019
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
Code:
'0001'

and have code section and then
Code:
 '0002'

and so on, where ever there is a .
Code:
.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
Code:
'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

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

# 5  
Old 08-05-2019
hmmm... not sure... I'll give it a whirl:
Code:
awk -F\' '/l_code_number.*:: *num *$/ {$2=($4==.001) ? last_field2 : ++start; $2=sprintf("%04d", $2); last_field2=$2;} 1 ' OFS=\' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Incrementing the New File Version Number

Hi, This is my first post here. I am using cygwin on Windows 7. I am starting with a data file with filename "name_1.ext", like "20180831_snapgenotypes_1.csv". The "_1" before ".ext" is a version number. Integers (0-99) are sufficient. They don't have to be like "1.0.0". The filename may... (2 Replies)
Discussion started by: minimalist
2 Replies

2. Shell Programming and Scripting

Incrementing parts of ten digits number by parts

I have number in file which contains date and serial number: 2013101000. The last two digits are serial number (00). So maximum of serial number is 100. After reaching 100 it becomes 00 with incrementing 10 which is day with max 31. after reaching 31 it becomes 00 and increments 10... (31 Replies)
Discussion started by: Natalie
31 Replies

3. Shell Programming and Scripting

Replacing string by incrementing number

Dear all Say I have a file as ! TICKET NBR : 234 !GSI : 102 ! 3100.2.112.1 11/06/2013 15:56:29 ! 3100.2.22.3 98 ! 3100.2.134.2 8 ! ! TICKET NBR : 1809 ! GSI : 102 ! 3100.2.112.1 11/06/2013 16:00:45 ! 3100.2.22.3 65 ! 3100.2.134.2 3 ! ! TICKET NBR : 587 ! GSI : 102 ! 3100.2.112.1... (3 Replies)
Discussion started by: OTNA
3 Replies

4. UNIX for Dummies Questions & Answers

Incrementing the New File Version Number

Hello All, In the below script i am trying to check and list the file names, get the last file with highest version number and then increment the version number when i create another file. Example: file1 is COBANK_v1.xml and file2 i want to put it as COBANK_v2.xml, to achieve this i am using awk... (15 Replies)
Discussion started by: Ariean
15 Replies

5. Shell Programming and Scripting

Incrementing number in bash

I have the following code and getting the error ./raytrac.bash: line 231: ((: 0++: syntax error: operand expected (error token is "+") iarg = 0 iarg=0 narg=$# # Number of arguments passed. echo "narg = $narg" argsArr=("$@") # Set... (1 Reply)
Discussion started by: kristinu
1 Replies

6. Shell Programming and Scripting

Replacing Port number Using Sed

Hi Im trying to replace http port value.However for some reason its not working.Can you guys take a look and hit me with suggestions please.Your help is much appreciated. echo "Enter Value" read ans sed -i "s/http-port = 80 /http-port = $ans/g" demo Note:demo is the filename ... (6 Replies)
Discussion started by: coolkid
6 Replies

7. Shell Programming and Scripting

Unix c-shell - replacing/incrementing values in columns?

2 21 1 12 3 123 4 1234 6 49 0 49 33 212 I need to replace/increment all the values in the 2nd column that correspond to 0 in the first column. so for 0 49 i would get 0 50 this can be done through: paste num4.txt... (14 Replies)
Discussion started by: audrey_flox
14 Replies

8. Shell Programming and Scripting

Replacing word and Incrementing

Hi I'm having difficulty in writing a script with searching a specified word using sed and replaces that word with numbers that is incremented I tried this: #!/bin/sh awk '{ for (i=2010; i<=NF; i++) sed 's/TBA/$i; }' filename.txt > outputfile.txt but it doesn't work. here is my desired... (1 Reply)
Discussion started by: sexyTrojan
1 Replies

9. Shell Programming and Scripting

Replacing the first 0 with a number

Hi, Appreciate any help in advance ;-) There are some numbers between 0 and 1, 0 not included: .2500, .3333, .5000, .6666, .7500, 1.000 How to replace the first 0 ( after the dot ) with a number, say, if the number is 3: .2500 will be .2530 .5000 will be .5300 .7500 will be .7530 ... (3 Replies)
Discussion started by: wanttolearn
3 Replies

10. Shell Programming and Scripting

perl + array and incrementing number

morning guys and gals, I am haveing a problem, a friend helped me out with this script but i dont know how to add incrementing number for each movie in movie.list. this is what i have so far. any assistance would be great. I have removed the GT and LT symbols so you can see what is going on... (5 Replies)
Discussion started by: Optimus_P
5 Replies
Login or Register to Ask a Question