计数字符串中的点的数量 [英] Counting the number of dots in a string

查看:97
本文介绍了计数字符串中的点的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么算点在BASH字符串中的数字?例如:

  VAR =S454-da4_sd.fs_84-df.f-sds.a_as_d.a-565sd.dasd#变量var包含5个点


解决方案

您可以做到这一点结合的grep 厕所命令:

 回声string.with.dots。 | grep的-o\\。 |厕所-l

说明:

 的grep -o#只会返回匹配符号线/由/线路
厕所-l#将通过计算产生的grep的行数

或者你可以只用的grep 用于这一目的:

 回声string.with.dots。 | grep的-o\\。 | grep的-c\\。

How do I count the number of dots in a string in BASH? For example

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

# Variable VAR contains 5 dots

解决方案

You can do it combining grep and wc commands:

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

Explanation:

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

Or you can use only grep for that purpose:

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

这篇关于计数字符串中的点的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆