Hi,
I would like to found a regex match in a stdout
stdout
/dev/loop0: [2081]:64 (/a/path/to/afile.dat)
I would like to match
/dev\/loop\d/
and return /dev/loop0
but the \d
seem not working with awk … ?
How to achieve this ? ( awk is not mandatory )
Assuming you made a bit of a typo with your regexp, any of these should work as you want:
grep -oE '/dev/loop[0-9]+' awk 'match($0, /\/dev\/loop[0-9]+/) { print substr($0, RSTART, RLENGTH) }' sed -r 's%.*(/dev/loop[0-9]+).*%\1%'
AWK one is a bit cursed as you can see. Such ways of manipulating text is not exactly it’s strong suite.
You’ve got lots of answers, so I’ll just say that shorthand character classes like \s, \w, and \d - all those backslash ones - are not widely supported, especially in the only POSIX tools. Many tools have an extended or Perl mode that makes them available, but some don’t. You can’t rely on them being everywhere. That’s why you’re getting suggestions to use explicit, long-form character classes like [0-9].