第n个字符后猛砸子的indexOf [英] Bash indexof substring after nth character

查看:166
本文介绍了第n个字符后猛砸子的indexOf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让Bash的第n个字符后的子串的的indexOf,而无需逐个切原字符串,那将是漫长的。

is there a way to get indexOf of a substring after nth character in Bash, without needing to cut the original string one by one, that would be lengthy

              12345678901234
$ expr index 'abcdeabcdabcaa' 'c'
3
$ expr index 'abcdeabcdabcaa' 'ca'
1 

我想会是什么:

$ indexOf 'abcdeabcdabcaa' 'ca'
12
$ indexOfAfter 5 'abcdeabcdabcaa' 'c'
8
$ indexOfAfter 5 'abcdeabcdabcaa' 'ca'
12
$ indexOfAfter 9 'abcdeabcdabcaa' 'b'
11
$ indexOfAfter 111 'abcdeabcdabcaa' 'c'
0

大概已经有上发挥作用猛砸做到这一点..这是不是一个功课,只是出于好奇。

probably there are already function on Bash to do this.. this is not a homework, just out of curiosity..

推荐答案

AWK

文件的indexOf内容:

contents of file indexOf:

echo "$1" "$2" | awk '{print index($1,$2)}' 

例如。

 indexOf 'abcdeabcdabcaa' 'ca'
 12

文件indexOfAfter的内容:

contents of file indexOfAfter:

echo "$1" "$2" "$3" | \
awk '{s=substr($2,$1);posn=index(s,$3);if (posn>0) print $1+posn-1; else print 0;}'

这篇关于第n个字符后猛砸子的indexOf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆