In bash script,
I have a string which contains several words separated by one or more than one spaces. ie:
Name Age Sex ID AddressIf 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.
211 Answers
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 => Addressor, 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 1 $ export FOO="Name Age Sex ID Address"Replace *Age with Age -- this will remove anything before "Age":
$ echo ${FOO/*Age/Age}
Age Sex ID AddressGet anything before "Age"
$ echo ${FOO/Age*/}
NameGet the length of that string (which is the index of "Age"):
$ BEGIN=${FOO/Age*/}
$ echo ${#BEGIN}
7 1 This is a 7-years-old question, but some may need the answer in pure bash.
STRING="Name Age Sex ID Address"
INDEXOF_AGE=${#${STRING/Age*/}}
echo $INDEXOF_AGE 1 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
fiOutput
2 Name 9 Age 13 Sex 20 ID 29 Address 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:
- Use the inborn abilities of the for construct to split up an unquoted input by whitespace.
- 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 Try the following javascript oneliner in a shell (use javascript shell) :
$ js <<< "x = 'Name Age Sex ID Address'; print(x.indexOf('Age'));"
7Or with a here-doc:
js <<EOF
x = 'Name Age Sex ID Address';
print(x.indexOf('Age'));
EOF 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 (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
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:
(go to: "Variable expansion / Substring replacement")
(go to: "The cut command" and "wc invocation"
A mix of two previously given answers, using pure bash arrays and substring replacement.
The idea is to get a string of all words before the one you want, then count the number of words in that substring by making it into an array.
$ haystack="Name Age Sex ID Address"
$ words_before=( ${haystack%Age*} ) # truncate string, make array
$ echo ${#words_before[*]} # count words in array
1Of course Age can be stored in another variable needle, then use ${haystack%$needle*}. Expect problems if the word you search for is a subset of another word, in which case kopischke's answer is still working.
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 its string indexing at zero, therefore I added +1 to the end of the command.
1the size of the remaining string after the erasure using wc -c so that you dont need to create an intermediary string variable
str="Name Age Sex ID Address";
nIndex="`echo -n "${str/Age*/}" |wc -c`";
declare -p nIndexdeclare -- nIndex="7"
Btw, this may be useful info: