匹配如果变量的单词重复多次
我有这个变量:
>echo $br_name
srxa wan-a1 br-wan3-xa1 0A:AA:DD:C1:F1:A3 ge-0.0.3 srxa wan-a2 br-wan3-xa2 0A:AA:DD:C1:F2:A3 ge-0.0.3
我正在尝试创建一个有条件的,其中它检测到 ge-0.0.3 是否在我的变量中重复1次以上$ br_name
例如:
if [[ $br_name has ge-0.0.3 repeated more than one time ]]
then
echo "ge-0.0.3 is shown more than once"
else
:
fi
I have this variable:
>echo $br_name
srxa wan-a1 br-wan3-xa1 0A:AA:DD:C1:F1:A3 ge-0.0.3 srxa wan-a2 br-wan3-xa2 0A:AA:DD:C1:F2:A3 ge-0.0.3
I am trying to create a conditional where it detects whether ge-0.0.3 is repeated more than 1 time in my variable $br_name
For example:
if [[ $br_name has ge-0.0.3 repeated more than one time ]]
then
echo "ge-0.0.3 is shown more than once"
else
:
fi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
BASH的
=〜
正在使用扩展RE。Bash's
=~
is using extended RE.您可以使用
grep -o
仅打印匹配的短语。另外,请使用-f
确保它匹配文字字符,而不是porgex,其中。
和-
是特别复杂的模式,当然是 特别的可以删除
-f
并为GREP
编写适当的正则You can use
grep -o
to print only the matched phrase. Also use-F
to make sure that it matches literal characters instead of a regex where.
and-
are specialFor more complex patterns of course you can drop
-F
and write a proper regex forgrep
简单的单词
如果您的单词“简单”,则可以检测出事件的数量:
其中替换为相同但使用
\ n
,然后wc
计数行或您可以尝试其中之一:
复合物,
因为您的单词是“复杂”的,或者您需要一个模式,您将需要正则是:
< /> > script.sh
执行
grep
with grep with grep with grep grep
https://i.sstatic.net/f5z6w.png https:/ href =“ =“ nofollow noreferrer”>
simple word
If your word would be "easy", you can detect the occurrences count with:
In which the word is replace for the same but with
\n
and thenwc
count the linesor you can try one of these:
complex
Since your word is "complex" or you will want a pattern, you will need regex:
script.sh
execution
grep
With grep you can get the ocurrence count
假设您想对整个单词而不是子字符串进行字面的字符串匹配,那么这可能是您想要的:
Assuming you want to do a literal string match on whole words, not substrings, then this might be what you want: