gpt4 book ai didi

linux - Bash - 计算字符串中的子字符串

转载 作者:IT王子 更新时间:2023-10-29 00:48:36 25 4
gpt4 key购买 nike

我有点难以编写一个命令来计算子字符串在字符串中出现的次数。

与其运行 10 次以下的代码,我宁愿事先计算子字符串出现的次数并根据其结果调整“for”:

代码如下:

CommandResult="Interface    Chipset     Driver     mon0    Unknown      iwlwifi - [phy0]wlan0       Unknown     iwlwifi - [phy0]"

for i in `seq 0 9`;
do
InstanceID="mon"$i

if echo "$CommandResult" | grep -q "$InstanceID"; then
echo "found"
fi
done

如有任何帮助,我们将不胜感激!

谢谢,

最佳答案

请随意尝试获取号码:

echo "$CommandResult" | tr " " "\n" | grep -c "$InstanceID"

关于linux - Bash - 计算字符串中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26212889/

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