当前位置:网站首页>shell统计某个字符串最后一次出现的位置之前的所有字符串
shell统计某个字符串最后一次出现的位置之前的所有字符串
2022-07-22 10:03:00 【雅冰石】
需求:
我想统计某个ip所属网段。
示例:
有一台服务器ip是192.168.144.249,我想统计到192.168.144这个网段。
方法一:
strToCheck='192.168.144.251'
letpos=`echo "$strToCheck" | cut -d '.' -f 1,2,3`
echo $letpos
结果为192.168.144
方法二较复杂些:
#先统计最后一个.在哪个位置
strToCheck='192.168.144.249'
charToSearch='.'
letpos=`echo "$strToCheck" | awk -F ''$charToSearch'' '{printf "%d", length($0)-length($NF)}'`
#再统计该位置前面的字符串
echo ${strToCheck:0:$letpos}
结果192.168.144.
--本篇文章参考自:Shell脚本实现查找字符串中某字符最后出现的位置 / 张生荣
边栏推荐
- Definition of derived classes
- Pytest interface automated testing framework | pytest installation and rules
- CI24R1低成本2.4G无线收发器芯片替代XN297精简版SI24R1
- pytest接口自动化测试框架 | 接口自动化至yaml数据驱动
- 【带你学c带你飞】第3章 分支结构(练习3.1 简单的猜数游戏)
- Si12t touch key chip replaces tms12
- Pytest interface automated testing framework | pytest parameterization
- Force deduction solution summary 324 swing sequencing II
- 自然语言处理NLP文本分类顶会论文阅读笔记(二)
- Pytest interface automated testing framework | the difference between unittest and pytest
猜你喜欢
随机推荐
pytest接口自动化测试框架 | 接口自动化至yaml数据驱动
Synchronized与lock锁区别
MySQL 查看表结构最后变更时间
Access to derived class members
虚函数
08.01 邻接矩阵
树和二叉树:树的概念
记一次 .NET 某RFID标签管理系统 CPU 暴涨分析
相比技术与产品 倾听与意见才是Web3当下的首要任务
Sentry nodestore_ View data in node table
【commons-beanutils专题】004- BeanUtils 专题
世纪佳缘“大地震”
最强屏幕工具ShareX v14.0.1
SI14T触摸按键芯片兼容替代TMS12
他的一只鸟,卖了6000万 ——明清大家八大山人藏品发售
【commons-beanutils专题】005- ConvertUtils 专题
PHP Cookie
CCF 1-2题答题记录(2)
Why does additional currency issue cause inflation? How to cause
CI24R1低成本2.4G无线收发器芯片替代XN297精简版SI24R1