简体   繁体   English

grep regex解决方案,用于在字符串中查找3个或更多破折号

[英]grep regex solution for finding 3 or more dashes in a string

I'm trying to build a bash script that will look for the keychain folder on a mac and delete it when found using grep. 我正在尝试构建一个bash脚本,该脚本将在mac上查找keychain文件夹,并在使用grep找到该脚本时将其删除。 The challenging part is no 2 folders are alike. 具有挑战性的部分是没有2个文件夹是一样的。 An example is C0955E7D-DCA1-542B-B506-72A054DET9FA. 一个示例是C0955E7D-DCA1-542B-B506-72A054DET9FA。 They all contain 3 or more dashes in the name. 它们的名称中均包含3个或更多破折号。 Not sure how to grep for this using regex. 不知道如何使用正则表达式对此进行grep。 While in my "~/Library/Keychain/ directory, I was thinking something like... 在我的〜/ Library / Keychain /目录中,我在想类似...

ls | grep "*-*-*-*"

I was thinking that * would be wildcard and look for any amount of digits that will contain at-least 3 dashes between them. 我当时以为*将是通配符,并查找任意数量的数字,它们之间至少包含3个破折号。

I'm also opening to using python too (since it comes preinstalled on macs) but that seemed too complicated. 我也开放使用python(因为它已预装在macs上),但这似乎太复杂了。

You can try (\\w+-){3,}\\w+ 您可以尝试(\\w+-){3,}\\w+

Explanation : 说明

\\w+ : word characters 1 or more times \\w+ :单词字符1次或多次
- : hyphen - :连字符
(\\w+-){3,} : some word characters followed by hyphen 3 or more times (\\w+-){3,} :某些单词字符后跟连字符3次或更多次
\\w+ : word characters 1 or more times \\w+ :单词字符1次或多次

this will print all directory names with 3 or more dashes 这将打印所有带有3个或更多破折号的目录名称

find . -type d -exec sh -c '[ $(echo {} | grep -o - | wc -l) -ge 3 ]' \; -print

grep ".+-.+-.+-.+" would be more accurate in your case. grep“。+-。+-。+-。+”在您的情况下会更准确。

. = any character
+ = one or more times (matches any character, one or more times)
(* = zero or more times)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM