Using awk:
awk '{w=$1} END{print w}' file
OR using sed:
sed -n '$s/^\([^ ]*\).*$/\1/p' file
Question
I got a bash script and I started to write a changelog to keep track of changes.
I added in the script a function that echoes "Script version --> "
But I have to manually edit it.
So I would need a function to get the first word until space of the last line and store it as $CURRENTVERSION
Changelog syntax is:
v-0.3.2 Added x, y.
v-0.3.2.1 Fixed y.
v-0.4 Added z.
Briefly I would need to store v-0.4
as $CURRENTVERSION
Solution
Using awk:
awk '{w=$1} END{print w}' file
OR using sed:
sed -n '$s/^\([^ ]*\).*$/\1/p' file
OTHER TIPS
If you don't like awk or sed, you can do this:
tail -n1 file | cut -d' ' -f1
But really, awk and sed are better tools for the job...
bash:
read CURRENTVERSION rest < <(tail -1 changelog)
CURRENTVERSION=$( perl -anle 'print $F[0] if eof' changelog )
echo $CURRENTVERSION
v-0.4