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 )

  • learnbyexample@programming.dev
    link
    fedilink
    English
    arrow-up
    5
    ·
    21 hours 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
      link
      fedilink
      arrow-up
      4
      ·
      21 hours ago

      I wish there was one single unifying regex standard.

      (obligatory xkcd in 3…2…)

    • bizdelnick@lemmy.ml
      link
      fedilink
      arrow-up
      4
      ·
      23 hours 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
    link
    fedilink
    arrow-up
    5
    ·
    23 hours 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