Link to home
Start Free TrialLog in
Avatar of Share Point
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

Avatar of Norie
Norie

This bash script will increment the last number read from a file named 'config.txt' and write the result back to the file.
#!/usr/bin/bash

value=`cat config.txt`
  
IFS=' '

read -ra ADDR <<< "$value" # str is read into an array as tokens separated by IFS

# for testing only, can be removed
for i in "${ADDR[@]}"; do # access each element of array
    echo "$i"
done

ADDR[-1]=$((${ADDR[-1]}+1))

echo ${ADDR[*]}>config.txt

Open in new window

Avatar of Share Point

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
Is there always going to be only 4 numbers?

If there is try replacing -1 with 3.
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.
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.
ASKER CERTIFIED SOLUTION
Avatar of Norie
Norie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
awk '{$NF=$NF+1;print}' < config.txt > config.new
mv config.new config.txt

Open in new window

What do you expect would be the result of
123 54 654 99

123 54 654 100
Or
123 54 655 00
?
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.
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:
perl -lane '$F[-1]++;print join(" ",@F)' config.txt >config.new
mv config.{new,txt}

Open in new window

or in one line:
perl -i -lane '$F[-1]++;print join(" ",@F)' config.txt

Open in new window


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.
perl -i -lane '$F[-1]++;print join(" ",@F)' *.txt

Open in new window

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