20
regex and awk... (programming.dev)
submitted 5 days ago by Corsair@programming.dev to c/linux@lemmy.ml

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/loop0

but the \d seem not working with awk ... ?

How to achieve this ? ( awk is not mandatory )

top 9 comments
sorted by: hot top controversial new old
[-] learnbyexample@programming.dev 6 points 5 days ago

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 4 points 5 days ago

I wish there was one single unifying regex standard.

(obligatory xkcd in 3..2..)

[-] ik5pvx@lemmy.world 6 points 5 days ago

Use [0-9] to match the number

[-] bizdelnick@lemmy.ml 5 points 5 days ago

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 5 points 5 days ago

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 0 points 5 days ago* (last edited 4 days ago)

That would also print the colon

Edit: missed the separator token. Sorry guys

[-] mmmm@sopuli.xyz 3 points 5 days ago

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

[-] manxu@piefed.social 3 points 5 days ago

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

[-] learnbyexample@programming.dev 2 points 5 days ago

Why would it print the colon?

this post was submitted on 08 Jun 2025
20 points (95.5% liked)

Linux

55198 readers
915 users here now

From Wikipedia, the free encyclopedia

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).

Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.

Rules

Related Communities

Community icon by Alpár-Etele Méder, licensed under CC BY 3.0

founded 6 years ago
MODERATORS