Is there a magic char, like &, that will match on an address regexp in sed?

StackOverflow https://stackoverflow.com/questions/23670033

  •  23-07-2023
  •  | 
  •  

Frage

I would like to write a bash script that returns the path of a mount point for a network mount on a Mac. What I've come up with so far is the following:

mount | sed -n -e "/^\/\/mynetaddr/s/\/\/mynetaddr on \([^ ]*\).*$/\1/p"

What this does is look for a network resource //mynetaddr and grab the mount point path from the mount command.

Is there a magic char, like &, that will match on an address regexp in sed? Alternatively, how do I rewrite the above so that I do not have to repeat the network address?

War es hilfreich?

Lösung

Just get rid of the address regex entirely:

mount | sed -n -e "s/\/\/mynetaddr on \([^ ]*\).*$/\1/p"

The s/// command will then be matched against every line of the input, and only those in which a replacement occurs will be printed out (because that's how the p flag works).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top