开发者

Position of a string within a string using Linux shell script?

If I have the text in a shell variable, say $a:

a="The cat sat on the mat"

How can I search for "cat" and return 4 us开发者_如何学编程ing a Linux shell script, or -1 if not found?


With bash

a="The cat sat on the mat"
b=cat
strindex() { 
  x="${1%%"$2"*}"
  [[ "$x" = "$1" ]] && echo -1 || echo "${#x}"
}
strindex "$a" "$b"   # prints 4
strindex "$a" foo    # prints -1
strindex "$a" "ca*"  # prints -1


You can use grep to get the byte-offset of the matching part of a string:

echo $str | grep -b -o str

As per your example:

[user@host ~]$ echo "The cat sat on the mat" | grep -b -o cat
4:cat

you can pipe that to awk if you just want the first part

echo $str | grep -b -o str | awk 'BEGIN {FS=":"}{print $1}'


I used awk for this

a="The cat sat on the mat"
test="cat"
awk -v a="$a" -v b="$test" 'BEGIN{print index(a,b)}'


echo $a | grep -bo cat | sed 's/:.*$//'


This can be accomplished using ripgrep (aka rg).

❯ a="The cat sat on the mat"
❯ echo $a | rg --no-config --column 'cat'
1:5:The cat sat on the mat
❯ echo $a | rg --no-config --column 'cat' | cut -d: -f2
5

If you wanted to make it a function you can do:

function strindex() {
    local str=$1
    local substr=$2
    echo -n $str | rg --no-config --column $substr | cut -d: -f2
}

...and use it as such: strindex <STRING> <SUBSTRING>

strindex "The cat sat on the mat" "cat"
5

You can install ripgrep on MacOS with: brew install --formula ripgrep.


This is just a version of the glenn jackman's answer with escaping, the complimentary reverse function strrpos and python-style startswith and endswith function based on the same principle.

Edit: updating escaping per @bruno's excellent suggestion.

strpos() { 
  haystack=$1
  needle=$2
  x="${haystack%%"$needle"*}"
  [[ "$x" = "$haystack" ]] && { echo -1; return 1; } || echo "${#x}"
}

strrpos() { 
  haystack=$1
  needle=$2
  x="${haystack%"$needle"*}"
  [[ "$x" = "$haystack" ]] && { echo -1; return 1 ;} || echo "${#x}"
}

startswith() { 
  haystack=$1
  needle=$2
  x="${haystack#"$needle"}"
  [[ "$x" = "$haystack" ]] && return 1 || return 0
}

endswith() { 
  haystack=$1
  needle=$2
  x="${haystack%"$needle"}"
  [[ "$x" = "$haystack" ]] && return 1 || return 0
}


Most simple is - expr index "The cat sat on the mat" cat

it will return 5

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜