Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

In bash script,

I have a string which contains several words separated by one or more than one spaces. ie:

Name   Age Sex  ID         Address

If I want to find any of the word, for instance I want to find the index of word "Age", how can I do it?

Is there any command that will return the index number of the word I want directly?

Thanks.

share|improve this question
    
Does the solution have to be strictly in bash? Or can awk, grep, etc. be used? –  jftuga Jun 9 '12 at 6:57

8 Answers 8

up vote 4 down vote accepted

Bash performs word splitting in strings all by itself – in fact, more often than not, avoiding that is an issue, and the reason quoting is so important. It’s easy to leverage that in your case: just put your string into an array without quoting it – bash will use word splitting to separate the individual elements. Assuming your string is stored in the variable $str,

ar=($str) # no quotes!

will return an array of 5 elements. Your array index is your word index (counting up from 0, like in most scripting and programming languages), i.e. “Age” is accessed using

${ar[1]}  # 0 => Name, 1 => Age, 2 => Sex, 3 => ID, 4 => Address

or, if you need to find the element index by content, loop over the array, i.e.

function el_index {
    cnt=0; for el in "${ar[@]}"; do
        [[ $el == "$1" ]] && echo $cnt && break
        ((++cnt))
    done
}
el_index "Age" # => 1
share|improve this answer
    
wow...i didn't know that without the quotes then it would be an array. thanks! –  GJ. Jun 10 '12 at 0:08

If coreutils are available you can do it in following way:

echo ${str/Age//}| cut -d/ -f1| wc -w

Per MariusMatutiae request I'm adding an explanation how this 3 steps operation works:

echo ${str/Age//} 1. replace string which is being searched for unique char (in my case /)

cut -d/ -f1 2. cut off whole part of string which is after unique char

wc -w 3. count and print words which are left this will give us an index number

For references please check:

http://www.tldp.org/LDP/abs/html/parameter-substitution.html (go to: "Variable expansion / Substring replacement")
http://www.gnu.org/software/coreutils/manual/coreutils.html (go to: "The cut command" and "wc invocation"

share|improve this answer
    
While this solves the problem at hand, such terse replies are frowned upon in these sites. It would be more helpful to spend a few words explaining exactly why this works. Please do so. –  MariusMatutiae Jul 9 at 10:49

I found a solution that works fine.

$ string='now is the time'
$ buf=the${string#*the}
$ echo $buf
output: the time
$ index=$(( ${#string} - ${#buf} + 1 ))
$ echo $index
output: 8 -> index of first word "the"

It works similar as function indexOf() in Java which returns first occurrence of an input string.

Found this solution in here http://www.linuxquestions.org/questions/linux-newbie-8/bash-string-manipulation-help-670627/ (last post). This guy saved my day. Credit to him.

Faster way if you want to do substring from first indexof.

$ a="some long string"
$ b="ri"
$ echo ${a/*$b/$b}
ring
$ echo ${a/$b*/$b}
some long stri

http://stackoverflow.com/questions/10349102/shell-script-substring-from-first-indexof-substring

share|improve this answer

Try the following javascript oneliner in a shell (use javascript shell) :

$ js <<< "x = 'Name   Age Sex  ID         Address'; print(x.indexOf('Age'));"
7

Or with a here-doc:

js <<EOF
x = 'Name   Age Sex  ID         Address';
print(x.indexOf('Age'));
EOF
share|improve this answer

Note: Assuming here that by index you mean you want to know which word it is (starting from 0), not which character in the string the word starts on. Other answers address the latter.

Not that I'm aware of, but you can make one. Two tricks:

  1. Use the inborn abilities of the for construct to split up an unquoted input by whitespace.
  2. Handle the case where you can't find the column you want. In this case, I chose to send the found index to stout and let the status code indicate whether the find was successful. There are other possibilities.

Code:

#!/bin/bash
find_index() {
    local str=$1
    local search=$2
    let local n=0
    local retval=1 # here, 1 is failure, 0 success
    for col in $str; do # $str unquoted -> whitespace tokenization!
    if [ $col = $search ]; then
        echo $n
        retval=0
        break
    else
        ((n++))
    fi
    done
    return $retval
}

test="Name   Age Sex  ID         Address"
idx=`find_index "$test" Age`
if [ $? -ne 0 ]; then
    echo "Not found!"
else
    echo "Found: $idx"
fi
share|improve this answer
$ export FOO="Name   Age Sex  ID         Address"

Replace *Age with Age -- this will remove anything before "Age":

$ echo ${FOO/*Age/Age}
Age Sex ID Address

Get anything before "Age"

$ echo ${FOO/Age*/}
Name

Get the length of that string (which is the index of "Age"):

$ BEGIN=${FOO/Age*/}
$ echo ${#BEGIN}
7
share|improve this answer

You can use bash's native regex

# a function to print the index of a field and its name
printIx() { 
  for ((l=0,i=1;i<$1;i++)) ;do 
     ((l+=${#BASH_REMATCH[i]}))
  done
  printf '%3s %s\n' $l "$2"
}

#   Using a zero based index
#   "0----+----1----+----2----+----3----+----4"
str="  Name   Age Sex  ID         Address   "

if [[ $str =~ ^(\ *)(Name)(\ +)(Age)(\ +)(Sex)(\ +()ID)(\ +)(Address)\ *$ ]] ;then
  F=(Name Age Sex ID Address)
  f=(   2   4   6  8      10)  # regex back-references
  for ((g=0;g<${#f[@]};g++)) ;do
     printIx  ${f[g]} "${F[g]}"
  done 
fi

Output

  2 Name
  9 Age
 13 Sex
 20 ID
 29 Address
share|improve this answer

If you don't have to strictly use bash, but can use other programs commonly found on systems with bash then you could use something like this:

echo "Name   Age Sex ID  Addr" | python -c 'print(raw_input().index("Age"))+1'

Python starts it's string indexing at zero, therefore I added +1 to the end of the command.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.