regex and awk...
from Corsair@programming.dev to linux@lemmy.ml on 08 Jun 10:33
https://programming.dev/post/31833655

cross-posted from: 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/loop0

but the \d seem not working with awk … ?

How to achieve this ? ( awk is not mandatory )

#linux

threaded - newest

ik5pvx@lemmy.world on 08 Jun 10:54 next collapse

Use [0-9] to match the number

bizdelnick@lemmy.ml on 08 Jun 12:00 collapse

Or, alternatively, [[:digit:]], and dont’ forget to add a quntifier + to match multiple digits. See documentaion for details.

awk '/^\/dev\/loop[[:digit:]]+/{print}'
mmmm@sopuli.xyz on 08 Jun 11:20 next collapse

Not sure if I’m understanding, but can’t you just pipe the whole thing to awk and capture the first field? Like

echo “/dev/loop0: [2081]:64 (/a/path/to/afile.dat)” | awk -F: ‘{print $1}’

Which would print

/dev/loop0

lungdart@lemmy.ca on 08 Jun 13:02 collapse

That would also print the colon

Edit: missed the separator token. Sorry guys

manxu@piefed.social on 08 Jun 13:09 next collapse

The field separator is declared to be the colon, with -F:, so the fields end and start at colons.

learnbyexample@programming.dev on 08 Jun 13:23 next collapse

Why would it print the colon?

mmmm@sopuli.xyz on 08 Jun 14:10 collapse

No, because we’re telling to use : as a separator with the -F flag

learnbyexample@programming.dev on 08 Jun 13:22 collapse

Regex syntax and features vary between implementations. \d isn’t supported by BRE/ERE flavors.

GNU grep supports PCRE, so you can use grep -oP ‘/dev/loop\d’ or grep -o ‘/dev/loop[0-9]’ if you are matching only one digit character.

4am@lemm.ee on 08 Jun 14:02 collapse

I wish there was one single unifying regex standard.

(obligatory xkcd in 3…2…)