匹配如果变量的单词重复多次

发布于 2025-01-25 10:31:26 字数 410 浏览 3 评论 0原文

我有这个变量:

>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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光无声 2025-02-01 10:31:26

BASH的=〜正在使用扩展RE。

[Bash-5.2] % check() { local s='(ge-0\.0\.3.*){2,}'; [[ "$1" =~ $s ]] && echo yes || echo no; }
[Bash-5.2] % check 'xxx'
no
[Bash-5.2] % check 'ge-0.0.3'
no
[Bash-5.2] % check 'ge-0.0.3 ge-0.0.3 '
yes
[Bash-5.2] % check 'ge-0.0.3 ge-0.0.3 ge-0.0.3 '
yes

Bash's =~ is using extended RE.

[Bash-5.2] % check() { local s='(ge-0\.0\.3.*){2,}'; [[ "$1" =~ $s ]] && echo yes || echo no; }
[Bash-5.2] % check 'xxx'
no
[Bash-5.2] % check 'ge-0.0.3'
no
[Bash-5.2] % check 'ge-0.0.3 ge-0.0.3 '
yes
[Bash-5.2] % check 'ge-0.0.3 ge-0.0.3 ge-0.0.3 '
yes
笔芯 2025-02-01 10:31:26

您可以使用grep -o仅打印匹配的短语。另外,请使用-f确保它匹配文字字符,而不是porgex,其中-

if [[ $(echo "$br_name" | grep -Fo ge-0.0.3 | wc -l) -gt 1 ]]; then
    echo "ge-0.0.3 is shown more than once"
else
    echo "only once"
fi

特别复杂的模式,当然是 特别的可以删除-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 special

if [[ $(echo "$br_name" | grep -Fo ge-0.0.3 | wc -l) -gt 1 ]]; then
    echo "ge-0.0.3 is shown more than once"
else
    echo "only once"
fi

For more complex patterns of course you can drop -F and write a proper regex for grep

溺渁∝ 2025-02-01 10:31:26

简单的单词

如果您的单词“简单”,则可以检测出事件的数量:

echo "123 123 123" | sed "s/123 /123\n/g" | wc -l

其中替换为相同但使用\ n,然后wc计数行

或您可以尝试其中之一:

复合物,

因为您的单词是“复杂”的,或者您需要一个模式,您将需要正则是:

< /> > script.sh

count=0

for word in $1; do
  if [[ "$word" =~ .*ge-0\.0\.3.* ]]
  then
    count=$(($count+1))
  fi
done

if [ "$count" -gt "1" ];
then
  echo "ge-0.0.3 is shown more than once"
else
  if [ "$count" -eq "0" ];
  then
    echo "ge-0.0.3 is not shown"
  else
    echo "ge-0.0.3 is shown once"
  fi
fi

执行

bash script.sh "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"

“

grep

with grep with grep with grep grep

ocurrences=( $(grep -oE '(ge-0\.0\.3)' <<<$1) )
ocurrences_count=${#ocurrences[*]}
echo $ocurrences_count

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:

echo "123 123 123" | sed "s/123 /123\n/g" | wc -l

In which the word is replace for the same but with \n and then wc count the lines

or you can try one of these:

complex

Since your word is "complex" or you will want a pattern, you will need regex:

script.sh

count=0

for word in $1; do
  if [[ "$word" =~ .*ge-0\.0\.3.* ]]
  then
    count=$(($count+1))
  fi
done

if [ "$count" -gt "1" ];
then
  echo "ge-0.0.3 is shown more than once"
else
  if [ "$count" -eq "0" ];
  then
    echo "ge-0.0.3 is not shown"
  else
    echo "ge-0.0.3 is shown once"
  fi
fi

execution

bash script.sh "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"

demo

grep

With grep you can get the ocurrence count

ocurrences=( $(grep -oE '(ge-0\.0\.3)' <<<$1) )
ocurrences_count=${#ocurrences[*]}
echo $ocurrences_count

enter image description here

雨夜星沙 2025-02-01 10:31:26

假设您想对整个单词而不是子字符串进行字面的字符串匹配,那么这可能是您想要的:

$ if (( $(grep -oFw 'ge-0.0.3' <<<"$br_name" | wc -l) > 1 )); then echo 'yes'; else echo 'no'; fi
yes

Assuming you want to do a literal string match on whole words, not substrings, then this might be what you want:

$ if (( $(grep -oFw 'ge-0.0.3' <<<"$br_name" | wc -l) > 1 )); then echo 'yes'; else echo 'no'; fi
yes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文