I am trying to search for a text in a file and store it into a variable.
My file looks something like this:
Type: Furniture
Color: Blue
Version: 1
I am trying to search for the Version number and store it into a variable and increment it. This number is unknown to me when I am searching the file. So far I have something like this.
VERSIONNUMBER=//Somehow I want to get the version # from the file.
VERSIONNUMBER=`expr $VERSIONNUMBER + 1'
sed -i "s/Version:.*/Version=$VERSIONNUMBER/" filename
Am I on the right track so far or is there a better way of doing this? I’m having trouble searching for the version number. I keep getting syntax errors. Any help is much appreciated.
VERSIONNUMBER=`sed -i "s/Version:/*" filename`
This is a job for awk
Example
Explanation
/Version:/Match only lines that contain the text ‘Version:’$2++: Get the 2nd field, of which awk delimits with a space by default, and increment it{...}1: A shortcut to tell awk toprintout the lines after it does any alterations