Edit: I've borrowed the -r
flag (enables extended regex syntax) from jimmij to cure backlashitis.
The following works, under the following conditions:
- you are willing to say that the field separator is at least n spaces, e.g. 3
- the contents of the field of interest do not include a space anywhere.
In that case, this regex works:
echo ' 01 Title Chapter 01' |
sed -r 's/^.* {3,}([^ ]+) {3,}.*$/\1/'
Or, in case you like your backslashes, this is what this looks like in non-extended regex syntax:
echo ' 01 Title Chapter 01' |
sed 's/^.* \{3,\}\([^ ]\+\) \{3,\}.*$/\1/'
Explanation of the regex:
^ start of line
.* any number of characters at the start of the line
{3,} at least 3 spaces
([^ ]+) 1 or more non-space characters (capture this group as \1)
{3,} at least 3 spaces
.* anything on the rest of the line
$ end of the line. Not needed, because of the .*, but nicely explicit.
kkk 111 fff aaabbb 5d98 ccc mmmppp 9369d
, result:aaabbb 5d98 ccc
– pr.nizar Jan 14 '15 at 16:11