LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices



Reply
 
Search this Thread
Old 04-30-2010, 01:37 AM   #1
mech123
Member
 
Registered: Dec 2008
Posts: 36

Rep: Reputation: 16
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
 
Old 04-30-2010, 02:03 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Close:

Code:
fileArray=(${fileArray[@]##END*})
 
1 members found this post helpful.
Old 04-30-2010, 04:06 AM   #3
mech123
Member
 
Registered: Dec 2008
Posts: 36

Original Poster
Rep: Reputation: 16
how can i retrieve indexnumber of all the strings which are starting from "END"
 
Old 04-30-2010, 04:37 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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"
 
1 members found this post helpful.
Old 04-30-2010, 08:03 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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"
 
Old 05-03-2010, 08:19 AM   #6
mech123
Member
 
Registered: Dec 2008
Posts: 36

Original Poster
Rep: Reputation: 16
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
 
Old 05-03-2010, 08:48 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help, going mad about array pointers and strings... haydari Programming 1 04-12-2007 07:05 PM
Array of Pointers to C Strings anamericanjoe Programming 7 12-13-2006 07:44 PM
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 01:22 PM
Join an array of strings with C? notsoevil Programming 3 06-20-2003 11:58 AM
[C programming] sorting strings in an array miguetoo Programming 1 05-26-2003 02:48 AM


All times are GMT -5. The time now is 10:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration