gpt4 book ai didi

regex - 从文件awk多个正则表达式匹配

转载 作者:行者123 更新时间:2023-12-03 09:55:43 25 4
gpt4 key购买 nike

我正在尝试将awk用于word-regexp bt,无法与其他模式一起用于/搜索。
我下面有一个名为/tmp/fstab的文件,从中我想从它们的第三列中匹配三个正则表达式模式/sap/sap-data/,但它们不适合/

$ cat /tmp/fstab

#
# Created by anaconda on Wed Apr 26 14:50:13 2017
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_system-lv_root / xfs defaults 0 0
UUID=90db8951-35c4-4b03-734b-e157a272cf53 /boot xfs defaults 0 0
/dev/mapper/vg_system-lv_tmp /tmp xfs defaults,nodev 0 0
/dev/mapper/vg_system-lv_usr /usr xfs defaults 0 0
/dev/mapper/vg_system-lv_var /var xfs defaults 0 0
/dev/mapper/vg_system-lv_varlog /var/log xfs defaults 0 0
/dev/mapper/vg_system-lv_varlogaudit /var/log/audit xfs defaults 0 0
/dev/mapper/vg_system-lv_swap swap swap defaults 0 0
/tmp /var/tmp none defaults,nodev,nosuid,bind 0 0
tmpfs /dev/shm tmpfs defaults,nodev,noexec,nosuid 0 0

UUID="dddd13632-d6cf-4ed0-89f0-1026ab2f15af" /sap xfs defaults 0 1
UUID="7b896cf9-f842-4d3f-ee33-76c01e71aa69" /sap-data xfs defaults 0 1
我的结果:
$ awk '$2 ~ /sap|sap-data/  && $3 ~ /xfs/'  /tmp/fstab
UUID="dddd13632-d6cf-4ed0-89f0-1026ab2f15af" /sap xfs defaults 0 1
UUID="7b896cf9-f842-4d3f-ee33-76c01e71aa69" /sap-data xfs defaults 0 1
我想要的:
$ awk '$2 ~ /sap|sap-data/  && $3 ~ /xfs/'  /tmp/fstab
/dev/mapper/vg_system-lv_root / xfs defaults 0 0
UUID="dddd13632-d6cf-4ed0-89f0-1026ab2f15af" /sap xfs defaults 0 1
UUID="7b896cf9-f842-4d3f-ee33-76c01e71aa69" /sap-data xfs defaults 0 1
感谢您的帮助。

最佳答案

您可以将其与 anchor 中的/包装器交替使用:

awk '$2 ~ /^\/(sap-data|sap)?$/ && $3 ~ /xfs/' /tmp/fstab
/dev/mapper/vg_system-lv_root /                       xfs     defaults        0 0
UUID="dddd13632-d6cf-4ed0-89f0-1026ab2f15af" /sap xfs defaults 0 1
UUID="7b896cf9-f842-4d3f-ee33-76c01e71aa69" /sap-data xfs defaults 0 1

正则表达式 ^\/(sap-data|sap)?$与开始的 /匹配,然后在结束之前匹配 sap-datasap的可选匹配项。可选的 (...)?匹配也允许它仅匹配 /

关于regex - 从文件awk多个正则表达式匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64323145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com