Still features I didn’t know about after 30+ years of using sed. I was looking to verify if a path was an already mounted mount point:
Data:
$ path=/var/tmp
$ mount | grep " on $path "
rpool/joint/var/tmp on /var/tmp type zfs (rw,nodev,noatime,xattr,posixacl)
“Old way”
$ sedpath=${path//\//\\/}; echo $sedpath
\/var\/tmp
$ pathds=$(mount | sed -n -E '/^[^ ]+ on '$sedpath' /s/ .*//p'); echo $pathds
rpool/joint/var/tmp
“New to me way”, I already new I could used a different delimiters for s///, but didn’t know about the initial search.
$ pathds=$(mount | sed -n -E '\,^[^ ]+ on '$path' ,s, .*,,p'); echo $pathds
rpool/joint/var/tmp
If anyone knows how to simply get the sed to terminate after it prints one line in this manner, I would be interested to know, Only way I know would be
{s, .*,,p;Q}
but that uses GNU extension Q which I would rather avoid, and q doesn’t immediately terminate in this context, but it it better than
... | sed 1q
There are still “new” tricks to be learnt by old dogs, and I’m guessing the majority of sed users too. I’d be interested in how many people already knew of the \char pattern char
construct, and I supposed those who did not, who might remember it next time they are up against /patterhns/old/new/ and pattern is a patch which makes the match difficult, thus sedpath.