15
regex and awk... (programming.dev)

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 )

you are viewing a single comment's thread
view the rest of the comments
[-] TwilightKiddy@programming.dev 2 points 5 days ago

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.

this post was submitted on 08 Jun 2025
15 points (100.0% liked)

Linux

7853 readers
518 users here now

A community for everything relating to the GNU/Linux operating system

Also check out:

Original icon base courtesy of lewing@isc.tamu.edu and The GIMP

founded 2 years ago
MODERATORS