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

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

  •  23-07-2023
  •  | 
  •  

Pergunta

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?

Foi útil?

Solução

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).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top