New to me sed feature

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:


$ 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
$ pathds=$(mount | sed  -n -E '/^[^ ]+ on '$sedpath' /s/ .*//p'); echo $pathds

“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

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.

Hmm…might I recommend findmnt.

findmnt -no SOURCE /var/tmp

/var/tmp is just an example, could be anything else that wasn’t mount name related, for example gmail nested message labels have a / and I have to work around /pattern/s/a/b/ with pattern including /, so the sed work around to use \cpatterncscacbc will be useful too, and I can use the same pattern for grep -E and sed -E.

findmnt is certainly worth a look for my purposes though, thanks, another new trick. Given the 2010 Redhat copyright, it has been around for a while, and is available on arch, debian, and redhat, so should be generally useful.

Pity it doesn’t support zfs datasets as I’m writing a script to mount a alternate zfs root hierarchy, and there are multiple datasets with the same mountpoint but different hierarchy, and findmnt doesn’t see them unless they are mounted, But it will make some of the script checks simpler and cleaner, and add another tool to my toolbox.