get path from mount with sed

Viewed 106

so my mount looks like this

/dev/md0 on / type ext4 (rw,relatime,barrier,data=ordered)
none on /dev type devtmpfs (rw,nosuid,noexec,relatime,size=2983576k,nr_inodes=745894,mode=755)
none on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000)
none on /proc type proc (rw,nosuid,nodev,noexec,relatime)
none on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
/tmp on /tmp type tmpfs (rw,relatime)
/run on /run type tmpfs (rw,nosuid,nodev,relatime,mode=755)
/dev/shm on /dev/shm type tmpfs (rw,nosuid,nodev,relatime)
none on /sys/fs/cgroup type tmpfs (rw,relatime,size=4k,mode=755)
cgmfs on /run/cgmanager/fs type tmpfs (rw,relatime,size=100k,mode=755)
cgroup on /sys/fs/cgroup/cpuset type cgroup (rw,relatime,cpuset,release_agent=/run/cgmanager/agents/cgm-release-agent.cpuset,clone_children)
cgroup on /sys/fs/cgroup/cpu type cgroup (rw,relatime,cpu,release_agent=/run/cgmanager/agents/cgm-release-agent.cpu)
cgroup on /sys/fs/cgroup/cpuacct type cgroup (rw,relatime,cpuacct,release_agent=/run/cgmanager/agents/cgm-release-agent.cpuacct)
cgroup on /sys/fs/cgroup/blkio type cgroup (rw,relatime,blkio,release_agent=/run/cgmanager/agents/cgm-release-agent.blkio)
cgroup on /sys/fs/cgroup/memory type cgroup (rw,relatime,memory,release_agent=/run/cgmanager/agents/cgm-release-agent.memory)
cgroup on /sys/fs/cgroup/devices type cgroup (rw,relatime,devices,release_agent=/run/cgmanager/agents/cgm-release-agent.devices)
cgroup on /sys/fs/cgroup/freezer type cgroup (rw,relatime,freezer,release_agent=/run/cgmanager/agents/cgm-release-agent.freezer)
none on /proc/bus/usb type devtmpfs (rw,nosuid,noexec,relatime,size=2983576k,nr_inodes=745894,mode=755)
none on /sys/kernel/debug type debugfs (rw,relatime)
securityfs on /sys/kernel/security type securityfs (rw,relatime)
/dev/md2 on /volume1 type btrfs (rw,relatime,ssd,synoacl,space_cache=v2,auto_reclaim_space,metadata_ratio=50,subvolid=257,subvol=/@syno)
none on /config type configfs (rw,relatime)
/dev/sdu2 on /volumeUSB1/usbshare1-2 type fuseblk.ntfs (rw,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096)

what I'm after is this line:

/dev/sdu2 on /volumeUSB1/usbshare1-2 type fuseblk.ntfs (rw,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096)

and what I want from this line is this:

/volumeUSB1/usbshare1-2

I have the following regex, that works in PowerShell:

'/volumeUSB1/[^\s/]*'

However I'm trying to do everything in Bash now, and I'm a noob in Bash but I tried to play around with sed and grep like this:

  • mount|grep -e '/volumeUSB1/[^\s/]*'
  • mount|sed '/volumeUSB1/[^\s/]*'
  • mount|sed '\</volumeUSB1/[^\s/]*\>'

but none of these really worked. can someone help me?

2 Answers

With your shown samples, please try following. Written and tested in GNU awk.

your_command | awk 'match($0,/\/volumeUSB1\/[^ ]*/){print substr($0,RSTART,RLENGTH)}'

Explanation: Adding detailed explanation for above.

awk '                              ##Starting awk program from here.
match($0,/\/volumeUSB1\/[^ ]*/){   ##using match function to match regex \/volumeUSB1\/[^ ]* to match from /volumeUSB1/ till space comes.
  print substr($0,RSTART,RLENGTH)  ##Printing matched regex value.
}'

You can use

awk '$3 ~ /^\/volumeUSB1\//{print $3;exit}'
sed -n 's/.*\(\/volumeUSB1\/[^ ]*\).*/\1/p'
grep -o -m1 '/volumeUSB1/[^ ]*'

Split the line with whitespace as field separator and if Field 3 (Column 3) starts with /volumeUSB1/ print its value and exit.

The sed command means:

  • -n - suppresses default line output
  • s/.*\(\/volumeUSB1\/[^ ]*\).*/\1/ matches any text, then captures /volumeUSB1/ and any zero or more chars other than space into Group 1, and then matches the rest of the string and replaces the match with the contents of Group 1 and
  • p - prints the result of the substitution.

grep does the following

  • -o - only returns matches
  • -m1 - the first occurrence only
  • /volumeUSB1/[^ ]* - /volumeUSB1/ and then any chars other than a space.

You may replace [^ ] with [^[:space:]] to match any chars other than any whitespace.

See the online demos.

Related