I have a configuration file that contains a lookback value, which I need to increment by +1 each day.

IE: 60, 61, 62 etc....

It doesn't look like awk will work because of the formatting of the config file. I was thinking sed could maybe append it to the existing file, but then I will need to set up a counter to work with this operation.

Any ideas?

  • Extract the value from the file, increment it, replace it in the file. – devnull Feb 11 '14 at 19:27
  • 1
    Please show us your actual format. Also show how you create the file etc. We can't help if we don't know what your data look like. Are they comma separated values like in your example? – terdon Feb 11 '14 at 19:38
  • 1
    See my A to this Q. It shows how you can do the increment part: unix.stackexchange.com/questions/113971/…. Making it a cronjob or at job would do the "daily" part. – slm Feb 11 '14 at 19:39

Assuming this file only contains the text

IE: 60

Do increment it, I would do

read label number < filename
echo "$label $((number+1))" > filename

or maybe

perl -i -pe 's/(\d+)$/ $1 + 1 /e' filename

or maybe

awk '{$NF++; print}' filename > tmpfile && mv tmpfile filename

Intialise the counter file

echo "IE: 0 $(( $( date +%s ) /(60*60*24) ))" >cfile

cfile stores your label, a counter and a days since epoch value.

Increment value

read label a b <cfile ;incr=$(( $( date +%s ) /(60*60*24) - $b )); echo "$(( $label $a + $incr)) $(( $b + $incr ))" > cfile


echo "Today's values $( cat cfile )"

Storing both a count and a day value since epoch, allows it to be called,

  • Multiple times per day only incrementing on first call.
  • Several days later, values are incremented correct day difference since last call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.