Bash substring index after the nth character

Is there a way to get indexOf substrings after the nth character in Bash, without having to trim the source string one by one, which would be long

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

What I want will be:

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

there may already be a function on Bash to do this .. it's not homework, just out of curiosity.

+4
source share
1 answer

both operations are implemented using awk

contents of indexOf file:

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

eg.

  indexOf 'abcdeabcdabcaa' 'ca' 12 

contents of indexOfAfter file:

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

Source: https://habr.com/ru/post/1491808/


All Articles