Linux shell获得字符串所在行数及位置
【摘要】 shell 获得字符串所在行数及位置
01 获取字符串所在的行数
方式一:用grep -n
[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# cat test | grep -n exe
5:exe
[root@root]# cat test | grep -n e...
shell 获得字符串所在行数及位置
01 获取字符串所在的行数
方式一:用grep -n
[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]# cat test | grep -n exe
5:exe
[root@root]# cat test | grep -n exe | awk -F ":" '{print $1}'
5
方式二:用sed -n '/查询的字符串/=' 文件
[root@root]# cat test
apple
bit
create
delect
exe
flow
good
[root@root]#
[root@root]# sed -n '/exe/=' test
5
02 获取字符串中字符所在的位置
方式一:用awk -F
和 wc -c
组合
[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}';
uellevcmp
[root@root]# echo 'uellevcmpottcap' | awk -F 'ott' '{print $1}' | wc -c
10
方式二:用awk 'BEGIN{print index("'${str}'","'${str1}'") }'
[root@root]# str='uellevcmpottcap';str1='ott';awk 'BEGIN{print index("'${str}'","'${str1}'") }'
10
文章来源: blog.csdn.net,作者:小林coding,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/qq_34827674/article/details/99685026
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)