grep
can't be used to output parts of a match, but why not get rid of the parentheses?
This works for GNU grep
:
grep -P '\(\d+\)' -o demo.txt | sed 's/[()]//g' | sort -nr | head -n1
The following also work for BSD grep
, which you can use on OS X, for example:
grep -E '\([[:digit:]]+\)' -o demo.txt | … grep -E '\([0-9]+\)' -o demo.txt | …
To get the result we want, we add the -n
argument to sort
to sort numerically, which gives you 10
as the first result.