I am trying to extract U-Boot version from its binary for comparison,
consider that exact string what I want to search is as follow,
U-Boot 2013.07.010 (Mar 21 2016 - 12:07:48)
so I wrote command with regex as follows,
strings uboot | grep "U-Boot ([0-9]{4}.[0-9]{2}.[0-9]{3} ([a-z]{3} [0-9]{2} [0-9]{4} - [0-9]{2}:[0-9]{2}:[0-9]{2}))"
However I don’t see any output for that above command,
I could make below command work which extracts version only by comparing versions and not looking for date in the string
strings uboot | grep "U-Boot ([0-9]{4}.[0-9]{2}.[0-9]{3})"
Could someone correct/suggest what am I doing wrong in first command?
Is there a better way of doing the same ?
Advertisement
Answer
Your regex in 1st command is broken, you need to escape few chars like the dot for a proper match, also for matching the month i.e Mar
you need [A-Za-z]{3}
but you have specified only [a-z]{3}
Also you can use the -o
switch to just print out the matched part. From the man page of grep
:
-o, –only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.
To extract just the version use anyone of these:
$ grep -oP "(?<=^U-Boots)[0-9]+.[0-9]+.[0-9]+" <<< "U-Boot 2013.07.010 (Mar 21 2016 - 12:07:48)" 2013.07.010 $ grep -oP "(?<=^U-Boots)[0-9]{4}.[0-9]{2}.[0-9]{3}" <<< "U-Boot 2013.07.010 (Mar 21 2016 - 12:07:48)" 2013.07.010 $ egrep -o "b[0-9]{4}.[0-9]{2}.[0-9]{3}b" <<< "U-Boot 2013.07.010 (Mar 21 2016 - 12:07:48)" 2013.07.010 $ egrep -o "b[0-9]+.[0-9]+.[0-9]+b" <<< "U-Boot 2013.07.010 (Mar 21 2016 - 12:07:48)" 2013.07.010 $