Share Point
asked on
How to find and increment by number in shell script?
Hi Experts,
We do have one text file in linux host. The content of text file is number with few spaces
for example - 123 54 654 45
The requirement is to find the last number (45) and increment by 1 and save the file.
Can you please help us to find the Shell script command to accomplish this?
Thank You
We do have one text file in linux host. The content of text file is number with few spaces
for example - 123 54 654 45
The requirement is to find the last number (45) and increment by 1 and save the file.
Can you please help us to find the Shell script command to accomplish this?
Thank You
ASKER
Hi,
Thank you for the script. I am able to see the values in array but getting error " bad aaray subscript" on below command.
ADDR[-1]=$((${ADDR[-1]}+1))
Thank You
Thank you for the script. I am able to see the values in array but getting error " bad aaray subscript" on below command.
ADDR[-1]=$((${ADDR[-1]}+1))
Thank You
Is there always going to be only 4 numbers?
If there is try replacing -1 with 3.
If there is try replacing -1 with 3.
ASKER
Hi,
Also there must be space between numbers. I just need to find last space and increment by 1. Below are some example in text file. each text file must have one line of code.
123 45 67
156 56
123 67 678
Do you think code will work for all three criteria?
Thank you for helping me.
Also there must be space between numbers. I just need to find last space and increment by 1. Below are some example in text file. each text file must have one line of code.
123 45 67
156 56
123 67 678
Do you think code will work for all three criteria?
Thank you for helping me.
The code I posted will write the values back to the file with spaces between.
If there are going to be a different number of values and the -1 isn't working then another approach will be needed - I'll see what I can come up with.
If there are going to be a different number of values and the -1 isn't working then another approach will be needed - I'll see what I can come up with.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot Norie. I will test and let you know.
why not make it a plain number, increment, and then separate them?
xxx xx xxx xx?
xxx xx xxx xx?
awk '{$NF=$NF+1;print}' < config.txt > config.new
mv config.new config.txt
What do you expect would be the result of
123 54 654 99
123 54 654 100
Or
123 54 655 00
?
123 54 654 99
123 54 654 100
Or
123 54 655 00
?
ASKER
Hi Simon and Arnold
Thanks for your help. Norie solution worked for me but i have another ask so will create the new question.
Thank You All.
Thanks for your help. Norie solution worked for me but i have another ask so will create the new question.
Thank You All.
Personally I think Simon's solution was best. Simple, efficient, and concise (and I don't think this is a requirement, but if there's more than 1 line in the file then it will process all of them).
Here are 2 Perl alternatives:
Here are 2 Perl alternatives:
perl -lane '$F[-1]++;print join(" ",@F)' config.txt >config.new
mv config.{new,txt}
or in one line:perl -i -lane '$F[-1]++;print join(" ",@F)' config.txt
P.S. You can process as many files as you like at a time with my 2nd alternative, e.g. this processes all .txt files in the directory.
The '++' increment notation can also be used to abbreviate Simon's solution, though, like this:
perl -i -lane '$F[-1]++;print join(" ",@F)' *.txt
And like Simon's, if the input file file contains multiple lines, they will all be processed.The '++' increment notation can also be used to abbreviate Simon's solution, though, like this:
awk '{$NF++;print}' <...etc...
Open in new window