3

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

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Wyatt_LandWalker
  • 309
  • 1
  • 10

4 Answers4

6

Using awk:

awk '{w=$1} END{print w}' file

OR using sed:

sed -n '$s/^\([^ ]*\).*$/\1/p' file
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • LOL sed is way too complex, is there a wiki somewere? (I don't like too much to use what i don't know/understand pretty good) Tanks to the awk example you told me i would use this: currentver=(`awk 'END{print}' changelog.txt`) Cause i need to keep $1 empty, and then currentver[0] would be what i need; Is this a good way? – Wyatt_LandWalker Nov 12 '13 at 17:45
  • Yes awk is far more simple here. – anubhava Nov 12 '13 at 17:45
  • wouldn't `-r` with `sed` be better? it's won't require escaping brackets. – jkshah Nov 12 '13 at 17:49
  • Also `awk` will process entire file, isn't it? what if change_log is too long! just hypothetical query. I would have preferred `sed` or `tail` with `grep` – jkshah Nov 12 '13 at 17:50
  • 1
    I think you would need to measure performance to determine if the entire file was being processed to reach the end. In some sense, all line-oriented programs need to read from the beginning of the file to reach the end; binary search coupled with `seek()` could be used to start later, if you can determine that doing so would not miss any earlier lines required by the program. – chepner Nov 12 '13 at 19:12
3

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...

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
2

bash:

read CURRENTVERSION rest < <(tail -1 changelog)
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0
CURRENTVERSION=$( perl -anle 'print $F[0] if eof' changelog )
echo $CURRENTVERSION 
v-0.4
Kjetil S.
  • 3,468
  • 20
  • 22