bash - 计算字符串中的点数

标签 bash

如何计算 BASH 中字符串中的点数?例如

VAR="s454-da4_sd.fs_84-df.f-sds.a_as_d.a-565sd.dasd"

# Variable VAR contains 5 dots

最佳答案

您可以结合使用 grepwc 命令:

echo "string.with.dots." | grep -o "\." | wc -l

解释:

grep -o   # will return only matching symbols line/by/line
wc -l     # will count number of lines produced by grep

或者你可以只使用 grep 来达到这个目的:

echo "string.with.dots." | grep -o "\." | grep -c "\."

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

相关文章:

bash - 分配给位置参数

linux - bash 进程替换不能与 tee 一起正常工作

bash - sudo'd mysqldump over ssh 语法?

Bash 国家/地区代码转表情符号

bash - 根据匹配列合并多个文件

bash - 打印所有环境变量但跳过多行变量

linux - 在 bash 脚本中使用 grep 命令将变量值传递给 --include 参数

file - 是否可以合并 bash 和 awk 脚本文件?

bash - 检测/避免多次 apt-get/dpkg 安装

java - 无法在 Linux 中运行简单的 bash 命令,权限被拒绝