I have a certain file which contains many contents, including a line from the format (for example)
ABC = “x12”
What I need to do is to increment the value, so that after the command it will be
ABC = “x13”
The file itself needs to be updated, no need to create a new file or something similar.
I would appreciate if someone do this for me.
Advertisement
Answer
Here is my quick-and-dirty solution using sed
:
sed -i 's|([a-zA-Z]* = )"([A-Za-z]+)([0-9)]+)(")| echo 1 \"2$(expr 3 + 1 )\" |ge' ~/file.txt
Example:
$ cat ~/file.txt ABC = "x12" $ sed -i 's|([a-zA-Z]* = )"([A-Za-z]+)([0-9)]+)(")| echo 1 \"2$(expr 3 + 1 )\" |ge' ~/file.txt $ cat ~/file.txt ABC = "x13" $ # works for other keys as well: $ cat ~/file2.txt KEY = "y12" $ sed -i 's|([a-zA-Z]* = )"([A-Za-z]+)([0-9)]+)(")| echo 1 \"2$(expr 3 + 1 )\" |ge' ~/file2.txt $ cat ~/file2.txt KEY = "y13"