2

I have a configuration file that needs to be incremented daily.

E.g.

report.lookback.days.to.retrieve.data=60

This file contains multiple entries such as the one above that contains different values. I need to open the property file search for this specific entry, and increment the value by +1 each day for a total of 30 days.

E.g.

report.lookback.days.to.retrieve.data=61

report.lookback.days.to.retrieve.data=62

report.lookback.days.to.retrieve.data=63

I am new to scripting, and I'm not sure what method would accomplish this task.

Should I use awk, sed, implement a counter, or a combination of methods?

1
  • New to scripting should not be an excuse to not try anything by yourself. That said, if you try searching a bit then you'd find numerous examples of manipulating value in VAR=value form.
    – devnull
    Apr 14 '14 at 18:02
1

If you just want to edit the file in place each day, which you probably don't want to do, you could do something like this:

$ echo 'report.lookback.days.to.retrieve.data=61' \
    | awk -F= '
        /report.lookback.days.to.retrieve.data/{
            printf("%s=%d\n", $1, $2+1)
        }
    '
report.lookback.days.to.retrieve.data=62

But that would require saving to a temp file then replacing the original each time. As devnull mentioned, it would be better to replace the values with $vars and invoke the script with arguments.

1

If you just want to edit a file that looks like what you posted, use something like

perl -i  -pe 's/report.lookback.days.to.retrieve.data=\K\d+/$&+1/e' file

Explanation

The -i enables in-place editing, so the changes are saved to the original file, -p means "print each line after executing whatever script is givven by -e"/

The s/// is the substitution operator, it works like s/source/replacement.

The regular expression used is looking for the string report.lookback.days.to.retrieve.data= followed by one or more numbers (\d+). The \K discards whatever was matched before, allowing $&, whicih is a special variable holding whatever was just matched, to point to the numbers alone.

Finally, we replace it with $&+1, the value matched plus one. This (evaluating something within the s/// operator) is activated by the s///e flag at the end of the s///e.

0

This is very simple to do with awk, however the standard approach is to output to a temporary file and then replace the original, using Linux mktemp you can do:

tempfile=$(mktemp --tmpdir=.)
awk -F= '{
  if($1=="report.lookback.days.to.retrieve.data")
    print $1"="$2+1
  else
    print
  }' in_file >"$tempfile"
mv "$tempfile" in_file

Some versions of awk have the -i (inplace) option, meaning you can just do:

awk -iF= '{
  if($1=="report.lookback.days.to.retrieve.data")
    print $1"="$2+1
  else
    print
  }' in_file

Or if the sponge utility (part of moreutils) is available:

awk -F= '{
  if($1=="report.lookback.days.to.retrieve.data")
    print $1"="$2+1
  else
    print
  }' in_file |
sponge in_file

Of course if the input file doesn't contain any other lines apart from the one you want to increment (or if it does and you just want to delete them), then the awk part can be simplifed.

2
  • The tempfile awk solution worked! I can't thank you enough!
    – user65231
    Apr 15 '14 at 14:58
  • @user65231, the sponge solution is probably the best, sponge should be in the moreutils package. If this answer (or any other) is the one you are looking for, please take a moment to click the check mark and choose it as the solution.
    – Graeme
    Apr 15 '14 at 15:02

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.