LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 03-10-2011, 10:12 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Rep: Reputation: 16
Accessing arrays position in a split string


Hi,

I've a string "this.is.a.name", and I would like to put it in an array.
But, I've like to print the output of the array as:

Code:
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}
I've tried with

Code:
STR="this.is.a.name";
arr=$(echo $STR | tr "." " ");

echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}
but only the first position contains the phrase "this is a name". The rest of the array is empty.

I've searched over the manuals and the web, and I couldn't find a solution for my case.

How can I do that?

Thanks,
 
Old 03-10-2011, 10:18 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,158

Rep: Reputation: 247Reputation: 247Reputation: 247
You're close, but you need to add another set of () when you assign the array.

Code:
#!/bin/bash
STR="this.is.a.name"
array=($(echo $STR | tr "." " "))
echo ${array[0]}
echo ${array[1]}
echo ${array[2]}
echo ${array[3]}
 
Old 03-10-2011, 10:20 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Original Poster
Rep: Reputation: 16
That's right Guttorm, but what's the meaning of the outer parenthesis of "($(echo $STR | tr "." " "))"?

Last edited by xeon123; 03-10-2011 at 10:24 AM.
 
Old 03-10-2011, 10:25 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,158

Rep: Reputation: 247Reputation: 247Reputation: 247
Without the outer, the variable named array simply gets the value of "this is a name". The outer means it will be an array. The difference is the same as

array="this is a name"
array=(this is a name)
 
Old 03-10-2011, 10:36 AM   #5
xeon123
Member
 
Registered: Sep 2006
Posts: 364

Original Poster
Rep: Reputation: 16
Now I get it. Thanks for the help.
 
  


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
awk from end of string -3 position comparison dazdaz Linux - Software 4 10-04-2010 11:31 AM
[perl]How to treat string like "a b" as a single string when split? john.daker Programming 21 06-01-2009 06:57 PM
Using sed/awk to replace a string at a given position in anoopvraj Linux - Newbie 6 05-30-2009 08:59 AM
How to find a particular string occurance position in a file? vigneshinbox Programming 5 04-03-2009 12:52 AM
Replacing character position within the string of a file scroy Linux - Newbie 13 11-08-2008 06:45 PM


All times are GMT -5. The time now is 05:18 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