LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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, 12: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, 01:03 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,517

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
Close:

Code:
fileArray=(${fileArray[@]##END*})
 
1 members found this post helpful.
Old 04-30-2010, 03: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, 03: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,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
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, 07:03 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,517

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
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, 07: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, 07:48 AM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,517

Rep: Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896Reputation: 1896
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 06:05 PM
Array of Pointers to C Strings anamericanjoe Programming 7 12-13-2006 06:44 PM
Initializing Array of Strings jrtayloriv Programming 10 02-03-2005 12:22 PM
Join an array of strings with C? notsoevil Programming 3 06-20-2003 10:58 AM
[C programming] sorting strings in an array miguetoo Programming 1 05-26-2003 01:48 AM


All times are GMT -5. The time now is 09:30 PM.

Main Menu
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