Regex 使用sed从mount获取路径

Regex 使用sed从mount获取路径,regex,sed,Regex,Sed,所以我的挂载看起来像这样 /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

所以我的
挂载
看起来像这样

/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)
我想要的是这句话:

/dev/sdu2 on /volumeUSB1/usbshare1-2 type fuseblk.ntfs (rw,relatime,user_id=0,group_id=0,default_permissions,allow_other,blksize=4096)
我想从这一行得到的是:

/volumeUSB1/usbshare1-2
我有以下在PowerShell中工作的正则表达式:

'/volumeUSB1/[^\s/]*'
然而,我现在正在尝试在Bash中做所有的事情,我是Bash中的noob,但我尝试使用
sed
grep
如下:

  • mount | grep-e'/volumeUSB1/[^\s/]*'
  • mount | sed'/volumeUSB1/[^\s/]*'
  • mount|sed'\'
但这些都没有真正起作用。有人能帮我吗?

你可以用

awk'$3~/^\/volumeUSB1\/{print$3;exit}'
sed-n's/*\(\/volumeUSB1\/[^]*\)./\1/p'
grep-o-m1'/volumeUSB1/[^]*'
使用空格作为字段分隔符拆分行,如果字段3(第3列)以
/volumeUSB1/
开头,则打印其值并退出

sed
命令表示:

  • -n
    -抑制默认行输出
  • s/*\(\/volumeUSB1\/[^]*\)./\1/
    匹配任何文本,然后将
    /volumeUSB1/
    和除空格以外的任何零个或多个字符捕获到组1中,然后匹配字符串的其余部分,并用组1和组1的内容替换匹配项
  • p
    -打印替换结果
grep
执行以下操作

  • -o
    -仅返回匹配项
  • -m1
    -仅第一次出现
  • /volumeUSB1/[^]*
    -
    /volumeUSB1/
    然后是空格以外的任何字符
您可以将
[^]
替换为
[^[:space:]
,以匹配除空格以外的任何字符


请参阅。

中显示的示例,请尝试以下内容。用GNU
awk
编写和测试

your_command | awk 'match($0,/\/volumeUSB1\/[^ ]*/){print substr($0,RSTART,RLENGTH)}'
说明:添加上述内容的详细说明

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.
}'

谢谢,一切都很好。我使用哪种方法有关系吗?@SimonS
awk
任何类似的帖子都建议我使用
awk
。特别是如果你只需要一个匹配项,而不是使用mount |。。。您还可以在/proc/mount.on Linux上使用这些awk和sed建议,一种更好的方法是以机器可读的格式从
/proc
文件系统获取挂载点。@tripleee为什么这是一种更好的方法?因为让机器以人类可读的格式打印内容并将其解析回机器可读是低效且容易出错的。@tripleee抱歉,我是一个Windows人员。你是说像这样
cat/proc/mounts | awk'$3~/^\/volumeUSB1\/{print$3;exit}'
?这与解析
mount
命令的输出没有什么不同。我当时正在玩
/sys/fs
,但我的Linuxes是在macOS内部的Docker上运行的,所以我无法立即产生任何有用的东西。