cross-posted from: https://programming.dev/post/31833654
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/loop0but the
\dseem not working with awk ... ?How to achieve this ? ( awk is not mandatory )
9 Comments
ik5pvx@lemmy.world · 6 pts · 1y
Use [0-9] to match the number
bizdelnick@lemmy.ml · 5 pts · 1y
Or, alternatively,
[[:digit:]], and dont' forget to add a quntifier+to match multiple digits. See documentaion for details.mmmm@sopuli.xyz · 4 pts · 1y
Not sure if I'm understanding, but can't you just pipe the whole thing to
awkand capture the first field? Likeecho "/dev/loop0: [2081]:64 (/a/path/to/afile.dat)" | awk -F: '{print $1}'Which would print
/dev/loop0lungdart@lemmy.ca · 0 pts · 1y
That would also print the colon
Edit: missed the separator token. Sorry guys
manxu@piefed.social · 2 pts · 1y
The field separator is declared to be the colon, with -F:, so the fields end and start at colons.
mmmm@sopuli.xyz · 2 pts · 1y
No, because we're telling to use
:as a separator with the -F flaglearnbyexample@programming.dev · 1 pts · 1y
Why would it print the colon?
learnbyexample@programming.dev · 4 pts · 1y
Regex syntax and features vary between implementations.
\disn't supported by BRE/ERE flavors.GNU grepsupports PCRE, so you can usegrep -oP '/dev/loop\d'orgrep -o '/dev/loop[0-9]'if you are matching only one digit character.4am@lemm.ee · 4 pts · 1y
I wish there was one single unifying regex standard.
(obligatory xkcd in 3..2..)