Increment #'s in text file hourly

I have a text file with a number "001". I am trying to change this number every hour and increment by "1". So every hour it would add +1 to that number. I am able to change the file with sed but unable to have it increment it by "1" without me adding that to the sed command. Any help is appreciated.

sed 's/dist01/dist02/g' myfile

Thank You..

number=`grep -o [0-9]* myfile`
newnumber=$((${number}+1))
sed 's/${number}/${newnumber}/g' myfile
$ cat data.dat
001
$
#!/bin/sh

while true; do

oldnr=$(sed 's/^0*//' data.dat)
newnr=$(($oldnr+1))
printf "%03d\n" $newnr > data.dat
sleep 3600

done

There's no need for sed (or any external command):

file=data.dat
read num < "$file"
printf "%d\n" "$(( $num + 1 ))" > "$file"
1 Like

or eventually

#!/bin/sh

while true; do

oldnr=$(sed 's/^0*//' data.dat)
newnr=$(($oldnr+1))
newnrwithzeros=$(printf "%03d\n" $newnr)
sed -ie "s/.*/$newnrwithzeros/" data.dat
sleep 3600

done

Hi, Thank You for all the help so far. The problem I am having is getting this number to increment up by 1. I am not sure if its the "" but it does not seem to work correctly.

versionCode="1" and I need the 1 to increment +1 every time the script runs.

Thanks again.

versionCode=$(( $versionCode + 1 ))

Here is what I have:

#!/bin/bash

file=BuildManifest.xml
read num < "$file"
printf "%d\n" "versionCode=$(( $versionCode + 1 ))" > "$file"

but I keep getting this error in Terminal

build.sh: line 5: printf: versionCode=1: invalid number

Please put code inside

 tags.


#!/bin/bash

file=BuildManifest.xml
read num < "$file"
printf "%d\n" "versionCode=$(( $versionCode + 1 ))" > "$file"

but I keep getting this error in Terminal

build.sh: line 5: printf: versionCode=1: invalid number
[/quote]

[indent]
Is the file in Windows/DOS format with a CR at the end of the line? If so:

CR=$(printf "\r")
printf "%d\n" "versionCode=$(( ${versionCode%"$CR"} + 1 ))" > "$file"

That differs pretty much from your initial request, but here we go: first you'll have to pick the number out between the quotes
(following sed command deletes all but digits):

read num < "$file"
num=$(echo $num | sed 's/[^0-9]//g')
printf "Versioncode=\"%d\"\n" "$(( $num + 1 ))" > "$file"

Hi Guys, It changed the number. The only problem and I really apologize for not offering more information at the start is it prints the new # and thats it. I have a file with 10 lines of xml code and just wanted to change the version # and it wipes the file and just prints the

VersionCode="2" or whatever number it is at that time

The number changing is perfect :slight_smile: just need it to only change that. Any suggestions?

Can you post your current code which is working (apart from overwriting the input file and only printing VersionCode="2" in it)?
Can you post that 10 lines of xml code (the file that needs to be modified)?

#!/bin/bash

file=BuildManifest.xml
grep versionCode $file > vercheck.txt
vercheck=vercheck.txt
increase=increase.txt
read num < "$vercheck"
num=$(echo $num | sed 's/[^0-9]//g')
printf "versionCode=\"%d\"\n" "$(( $num + 1 ))" > "$increase"
sed -i 's/$vercheck/$increase/g' $file

Some of the XML:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:build="http://builds.builds.com"
      package="build"
      versionCode="1"
      versionName="1.0">
    <uses-permission name="build.permission.SEND" />
    <uses-permission name="build.permission.INTERNET" />
    <uses-permission name="build.permission.RECEIVE" />
    <uses-permission name="build.permission.READ_STATE" />

If I got you right, you only want to increase the versioncode number in the xml file, right?
If yes, try this:

If it works, then we can talk about unnecessary parts of the script...

It does not seem to be working. I looked in the vercheck.txt and it shows the version. The only thing is and I am not sure if this makes a difference but it has a lot of spaces:

  android:versionCode="1"

Thanks again for the help this forum really is awesome.

---------- Post updated at 02:10 AM ---------- Previous update was at 02:06 AM ----------

It does not seem to be changing the versionCode #.

Okay, can you try this (as you see I removed the last sed command, to see if the other parts work properly)?
If you are satisfied with the output of echo $increase then we can fully concentrate on the sed part.

#!/bin/bash

file=BuildManifest.xml
grep versionCode $file > vercheck.txt
vercheck=vercheck.txt
read num < "$vercheck"
num=$(echo $num | sed 's/[^0-9]//g')
increase=$(printf "versionCode=\"%d\"\n" "$(( $num + 1 ))")
echo $increase

That worked perfect! It increased the versionCode.

Let's call it perfect when the xml file get's correctly changed :wink:

Try this

#!/bin/bash

file=BuildManifest.xml
grep versionCode $file > vercheck.txt
vercheck=vercheck.txt
read num < "$vercheck"
num=$(echo $num | sed 's/[^0-9]//g')
increase=$(printf "versionCode=\"%d\"\n" "$(( $num + 1 ))")
num=$(grep versionCode $file)
sed -i "s/$num/$increase/g" $file

Until now, in the $vercheck variable there was the filename (vercheck.txt) stored, and not the actual string that needs to be replaced.
That might have been the failing point.

That didnt update the xml file.

Did you get any error message?
Can you please double check that you have correctly copy/pasted my latest posting (incl. some changes that I had to do right after I've posted that).

1 Like