Let’s write a shell script example to make note about how to find the first occurrence position and the last occurrence position of substring in string.

# the position index value start at 1
findCharvPos()
{
    str=$1
    str_find=$2
    # use regex by match to find param's position in $0. return the first occurrence position
    # pos=`echo $str | awk -v param=$str_find '{ printf( "%d\n", match( $0, param ) ) }'`
    # use field-separator to find param's position in $0. return the last occurrence position
    pos=`echo $str | awk -F ${str_find} '{print length($0)-length($NF)}'`
    echo $pos
}

# find the last occurrence positon of "v" in git tag's name
current_version="gitBranchTagv_v0.1.0"
vPos=$( findCharvPos $current_version "v" )
echo "vPos is "${vPos}
branch_tag_len=$(expr $vPos - 1)
echo "branch_tag_len is "${branch_tag_len}
eval "branch_tag=\${current_version:0:${branch_tag_len}}"
echo "branch_tag is "$branch_tag
eval "current_version=\${current_version:${vPos}}"
echo "current_version is "$current_version

Output:

[root@beaming-text-1 ~]# ./test.sh 
vPos is 15
branch_tag_len is 14
branch_tag is gitBranchTagv_
current_version is 0.1.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Tex To PDF
: convert the Latex file which suffix is tex to a PDF file

X
0
Would love your thoughts, please comment.x
()
x