Issue
I've a text file with this data
set( VERSION_MAJOR 1 )
set( VERSION_MINOR 2 )
set( VERSION_PATCH 3 )
I'm looking to make a string like this and save in a new file.
VERSION 1.2.3
I tried following code,
$ awk '/VERSION_MAJOR/ {print $3; exit} ' version.txt >> version.in
$ awk '/VERSION_MINOR/ {print $3; exit} ' version.txt >> version.in
$ awk '/VERSION_PATCH/ {print $3; exit} ' version.txt >> version.in
$ cat version.in
1
2
3
As you can see I can search/parse the individual versions, but unable to concatenate them in a single string such as 1.2.3
.
How can I do this with awk
or sed
?
Thank you.
Solution
Assumptions:
- there may be other lines in the file besides the 3x
VERSION_
entries - spacing is exactly as depicted in the example, ie, the desired numbers are in the 3rd space-delimited field
If you find yourself calling awk
multiple times chances are pretty good you can do the whole thing with a single awk
call; one idea combining OP's current code into a single awk
call:
awk '
/VERSION_MAJOR/ { maj=$3 }
/VERSION_MINOR/ { min=$3 }
/VERSION_PATCH/ { pat=$3 }
END { printf "VERSION %d.%d.%d\n", maj, min, pat }
' version.txt
This generates:
VERSION 1.2.3
NOTE: if one of the VERSION_
entries is missing then the %d
will cause the associated undefined awk
variable to display as a 0
Answered By - markp-fuso Answer Checked By - Dawn Plyler (WPSolving Volunteer)