您当前的位置: 首页 >  linux

小林coding

暂无认证

  • 2浏览

    0关注

    167博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

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

小林coding 发布时间:2019-08-16 20:32:47 ,浏览量:2

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
关注
打赏
1659932071
查看更多评论
立即登录/注册

微信扫码登录

0.0364s