grep
uses Posix Basic Regex (BRE
) by default which does not support your notation.
Use grep -E
to use Posix Extended Regex (ERE
) and grep -P
to use Perl Compatible Regex (PCRE
) if available.
Your notation works with grep -P
:
grep -P '^\s+version:\s+(\d\.\d\.\d)' file.txt
This works with BRE
:
grep '^ \+version: \+\([0-9]\.[0-9]\.[0-9]\)' file.txt
Output:
version: 1.2.3
Note, that the capture group is not necessary here, as grep
doesn't do anything with it.
If you want the version nr only., use \K
and -o
option:
grep -Po '^\s+version:\s+\K\d\.\d\.\d' file.txt
Output:
1.2.3
With BRE
, this is not possible, you will need to chain two grep
commands:
grep 'version: ' file.txt | grep -o '[0-9]\.[0-9]\.[0-9]'
or use sed
(credits @Kusalananda):
sed -n 's/.*version: //p' file.txt
grep
uses a very basic regex by default, your regex works withgrep -P
(at least when quoted...).\K
or(?<=...)
sed
:sed -n 's/.*version: //p' file
awk '/version/ { print $2 }' yourfile.txt