Linux shell 获得字符串所在行数及位置的方法
01获取字符串所在的行数
方式一:用grep-n
[root@root]#cattest
apple
bit
create
delect
exe
flow
good
[root@root]#cattest|grep-nexe
5:exe
[root@root]#cattest|grep-nexe|awk-F":"'{print$1}'
5
方式二:用sed-n'/查询的字符串/='文件
[root@root]#cattest 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{printindex("'${str}'","'${str1}'")}'
[root@root]#str='uellevcmpottcap';str1='ott';awk'BEGIN{printindex("'${str}'","'${str1}'")}'
10
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短