首页 > 系统服务 > 详细

Linux shell 获得字符串所在行数及位置

时间:2019-08-16 23:48:26      阅读:106      评论:0      收藏:0      [点我收藏+]

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 -Fwc -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

Linux shell 获得字符串所在行数及位置

原文:https://www.cnblogs.com/xiaolincoding/p/11366274.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!