Home > Net >  bash: Find the index of the first occurrence of a specific word in a string
bash: Find the index of the first occurrence of a specific word in a string

Time:12-22

Given a sentence with words delimited by spaces, how would i find the place of the first occurrence of a word?

e.g. the index of 'flower' in

sentence="That's a lovely flower you've got over there! Can i have a smell?"

should be 4

CodePudding user response:

Using awk and setting record separator as field separator

awk -v p='flower' 'BEGIN {RS=FS} $0 ~ p {print NR}' <<<"$sentence"
4

If you are not planning to use regexps, like for example

awk -v p='[Ff]lower' ...

or

awk -v p='smell[:punct:]*' ...

to allow punctuation chars, then you can just check for equality as @karakfa suggested

awk -v p='flower' 'BEGIN {RS=FS} $0==p {print NR}' 

also, if you want just the first occurence, add an exit

... {print NR; exit}'

CodePudding user response:

With sed:

sed 's/ /\n/g' <<<"$sentence" | sed -n "/flower/="

Output:

4

CodePudding user response:

You can use parameter expansion to remove the rest of the string to get the first word, compare it to the search term, and use parameter expansion to remove the first word from the string.

#! /bin/bash
search=flower
sentence="That's a lovely flower you've got over there! Can i have a smell?"

i=1
while [[ $sentence = *' '* ]] ; do
    if [[ $search = ${sentence%% *} ]] ; then  # Remove from the first space.
        echo $i
        break
    fi
    sentence=${sentence#* }                    # Remove the first word.
    ((   i ))
done

Or, you can populate an array with the words and iterate over the array:

read -ra words <<< "$sentence"
for i in "${!words[@]}" ; do
    if [[ ${words[i]} = $search ]] ; then
        echo $(( i 1 ))
        break
    fi
done

Or, you can select the part before the word, count the spaces in it (by removing everything that's not a space and checking the length of the remaining string) and add 2.

before=${sentence% $search *}
if [[ $before = $sentence ]] ; then
    echo Not found >&2
else
    spaces=${before//[^ ]}
    echo $(( 2   ${#spaces} ))
fi

Or...

CodePudding user response:

using multiple tools...

$ echo "$sentence" | tr ' ' \\n | grep -nxF flower | cut -d: -f1
4

CodePudding user response:

With ruby:

ruby -ane 'puts $F.index("flower") 1' <<< "$sentence"
4

CodePudding user response:

Try this, using combination of grep with head and awk

 grep 'flower' sampl.txt|head -1| awk '{split($0,data," ");for (word in data) {if (data[word]~/flower/){print word; break;}}}'

CodePudding user response:

With GNU sed

sed '/\bflower\b.*/!d;s///;s/[^ ]//g' <<< "$sentence" | wc -c

This assumes there is exactly one space between two words and no leading spaces in the $sentence.

  •  Tags:  
  • bash
  • Related