LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Array and check strings (https://www.linuxquestions.org/questions/linux-newbie-8/array-and-check-strings-805093/)

mech123 04-30-2010 12:37 AM

Array and check strings
 
I have array like fileArray=(AB CD EF GH END,4 IJ KL END,7)
Now i want outputarray like this (AB CD EF GH IJ KL)
Means if array contents start with END then delete that content.
I tried this by fileArray=($fileArray[@]#END) But it gives output fileArray=(AB CD EF GH ,4 IJ KL ,7)
How can i check this

grail 04-30-2010 01:03 AM

Close:

Code:

fileArray=(${fileArray[@]##END*})

mech123 04-30-2010 03:06 AM

how can i retrieve indexnumber of all the strings which are starting from "END"

catkin 04-30-2010 03:37 AM

Code:

#!/bin/bash

fileArray=(AB CD EF GH END,4 IJ KL END,7)
END_idxs=
for (( i=0; i<${#fileArray[*]}; i++ ))
do
  [[ "${fileArray[i]}" =~ ^END ]] && END_idxs="$END_idxs $i"
done

echo "Indexes are$END_idxs"


grail 04-30-2010 07:03 AM

Code:

#!/bin/bash

var=(AB CD EF GH END,4 IJ KL END,7)

index=$(echo ${var[@]} | awk '/^END/{print NR-1}' ORS=" " RS=" ")

echo "Indexes are $index"


mech123 05-03-2010 07:19 AM

I am not able to understand this stmt :--
[[ "${fileArray[i]}" =~ ^END ]]

Can you please elaborate this :-
Is above stmt. a syntax corresponding to if condition?

I used if like this :-
if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi

grail 05-03-2010 07:48 AM

Quote:

[[ "${fileArray[i]}" =~ ^END ]]
Does the i'th element of array fileArray contain (=~) the string "END" at the start (^)

[[]] - are test brackets and yes they act like an if whereby followed by && means do the next bit if test is true or followed
by || means do the next bit if false


All times are GMT -5. The time now is 06:42 AM.