linux - 从字符串中grep一个字符/单词并计算Linux中出现的次数
问题描述
我喜欢从字符串中提取一个字符然后计算它,我没有从谷歌搜索中看到。请指教。我可能会错过搜索它。
node_count=`echo "test1|test2|test3" | grep "|" |wc -l`|echo $node_count
对我来说,输出总是 1。
1
请记住,我不是从文件中 grep,而是从一行字符串中 grep。文件中的 grep 很容易。
解决方案
您可能想使用以下选项-o
:grep
$ node_count=`echo "test1|test2|test3" | grep "|" -o |wc -l` && echo $node_count
# 2
推荐阅读
- python - 将 Otree 项目连接到 MySQL
- go - golang模板 - 默认的语义如何
- java - FirebaseAuth 正在更改我当前的身份验证 UID
- java - 如何从 feign 客户端请求中获取经过的时间?
- typeform - 使用 TypeForm 从 url 获取参数值
- java - 将 Java 代码转换为 Kotlin 以实现 OnChangeListener 时遇到问题
- javascript - 从源 URL 获取即时 XML 更新
- r - R:在斜线后切断字符串
- corda - FlowException 与 CordaRuntimeException
- haskell - 我将如何使用每次传入都会重置的超时进行管道传输?