LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 09:12 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

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, 09:18 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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, 09:20 AM   #3
xeon123
Member
 
Registered: Sep 2006
Posts: 374

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 09:24 AM.
 
Old 03-10-2011, 09:25 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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, 09:36 AM   #5
xeon123
Member
 
Registered: Sep 2006
Posts: 374

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:53 AM.

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
Open Source Consulting | Domain Registration