LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-19-2009, 03:13 PM   #1
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Rep: Reputation: 0
Reading an array from another file


Hello, I am very new to Linux so please bear with me. Onto the problem:

I have created an array in a file (let's call this file file1). I then create another file (file2) to run a few scripts. My question is:

How to you access the array created in file1 from file2?

Any help would be greatly appreciated. Thank you for your time.
 
Old 02-19-2009, 03:27 PM   #2
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Okay, well to help you, we're going to need you to answer a few questions.

1. What programming language are you using?
2. What format are these arrays in?
 
Old 02-19-2009, 04:05 PM   #3
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PatrickNew View Post
Okay, well to help you, we're going to need you to answer a few questions.

1. What programming language are you using?
2. What format are these arrays in?
Thank you for your quick response. To answer your questions, I am programming in BASH and the format of the arrays are as follows:
arrayname=(arrayelements)

Last edited by nobody123; 02-19-2009 at 04:06 PM. Reason: forgot an '=' sign
 
Old 02-19-2009, 04:09 PM   #4
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Okay, so we have the name, followed by the list in parentheses. Are these elements separated by commas, tabs, spaces?

In bash, most array handling is kinda ad-hoc. By that I mean that it depends a lot on what you want to do. Bash doesn't really have built in arrays. So do you want to iterate over each element and execute some command? Or do you want to add them up? Or something else?
 
Old 02-19-2009, 04:19 PM   #5
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PatrickNew View Post
Okay, so we have the name, followed by the list in parentheses. Are these elements separated by commas, tabs, spaces?

In bash, most array handling is kinda ad-hoc. By that I mean that it depends a lot on what you want to do. Bash doesn't really have built in arrays. So do you want to iterate over each element and execute some command? Or do you want to add them up? Or something else?
The elements are indeed separated by spaces. What I want to do with the array is read the array elements and execute a command (e.g. using the 'grep' command to search for a specific value and display).
 
Old 02-19-2009, 04:25 PM   #6
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Code:
for X in `sed name_of_the_file -r -e 's/.*\((.*)\)/\1/g' | tr -d ','`
do
   #Your command here, where $X is replaced with this entry in the array
done
This takes the file and the sed command strips off the name of the array and the parentheses. The output of that is then sent (because of the |) to tr -d ',' which pulls off all the commas, so that now you just have numbers separated by spaces.

the
for [variable name] in [series of values separated by spaces]
do
#stuff
done

is the bash for-each loop.
 
Old 02-19-2009, 04:44 PM   #7
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PatrickNew View Post
Code:
for X in `sed name_of_the_file -r -e 's/.*\((.*)\)/\1/g' | tr -d ','`
do
   #Your command here, where $X is replaced with this entry in the array
done
This takes the file and the sed command strips off the name of the array and the parentheses. The output of that is then sent (because of the |) to tr -d ',' which pulls off all the commas, so that now you just have numbers separated by spaces.

the
for [variable name] in [series of values separated by spaces]
do
#stuff
done

is the bash for-each loop.
Thank you for your piece of code. There are a few questions I have about it though:
  1. Is it really necessary to have commas in an array? I have not been using any. If not, it is necessary to have | tr -d ','?
  2. "Where X is replaced with this entry in the array"...does that mean an array element is supposed to be in 'X'?

Here is how I have been creating my arrays as of late:
Code:
artist = ( “The Beatles” “Elvis Presley” “Bing Crosby” “Michael Jackson” “AC/DC” “ABBA” “Bee Gees” “Bob Marley” “Cliff Richard” “Led Zeppelin” )
And yes, I am trying to read string values from the array. I do hope this does not make anything problematic. Thank you again for helping me
 
Old 02-19-2009, 04:59 PM   #8
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Haha, no that is my mistake. I forgot that you said they were separated by spaces, I thought they were separated by commas. So yes - if you leave out the spaces, you can also leave out the tr -d ','.

Now I didn't realize that the elements of the arrays have spaces within them. That breaks the for - loop because it will try to call on things like '"The', 'Beatles"', '"Elvis', 'Presley"' and so on. Hrm, we'll need a new approach.

I'll think about this, and get back.
 
Old 02-19-2009, 05:00 PM   #9
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Do you put multiple arrays in one file? The whole problem gets much easier if the entries are one per line.
 
Old 02-19-2009, 05:00 PM   #10
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PatrickNew View Post
Haha, no that is my mistake. I forgot that you said they were separated by spaces, I thought they were separated by commas. So yes - if you leave out the spaces, you can also leave out the tr -d ','.

Now I didn't realize that the elements of the arrays have spaces within them. That breaks the for - loop because it will try to call on things like '"The', 'Beatles"', '"Elvis', 'Presley"' and so on. Hrm, we'll need a new approach.

I'll think about this, and get back.
Thanks again for your help. Please take your time!
 
Old 02-19-2009, 05:16 PM   #11
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by PatrickNew View Post
Do you put multiple arrays in one file? The whole problem gets much easier if the entries are one per line.
Yes, I do put multiple arrays in one file. I do believe I have been putting the arrays one per line (I do have more than one array. Here they are if you are interested in addition to the previous array stated:

Code:
CDName=( “Please Please Me” “Loving You” “Seasons” “Thriller” “Black Ice” “18 Hits” “Man To Man” “Two's Company” “Coda” )

PurchaseDate=( “11/12/2002” “9/23/1988” “1/1/2000” “6/15/1993” “12/19/1990” “3/30/1987” “5/20/2004” “2/19/1995” “5/8/2005” “7/31/2007” )

Cost=( “19.87” “20.00” “21.35” “18.21” “25.99” “30.00” “12.85” “18.00” “25.61” “10.21” )
 
Old 02-20-2009, 03:29 PM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If that is the actual form of the file you saved, the lines are also legal bash statements, and you can simply source it.

You are using a strange double quote. If you convert them to simple double quotes `"', then simply run:
. yourfile

After that, you can access the arrays normally.
. tempfile.sh
jschiwal@hpmedia:~> echo ${CDName[0]}
Please Please Me
jschiwal@hpmedia:~> echo ${CDName[1]}
Loving You

Here is an example where I used sed to use the NAME=VALUES results from ogginfo. I eval the results to set variables for a ogg2mp3 script.
Code:
cat bin/get_tags
get_tags()
{
    # clear out old tags
    ALBUM='';
    ARTIST='';
    DATE='';
    GENRE='';
    TITLE='';
    # the tags returned by ogginfo are of the form NAME=VALUE.
    # put single quotes around the values to encapsulate white space.
    # eval will set the tag variables ALBUM, ARTIST, DATE, GENRE, TITLE
    eval $(ogginfo ${1} | sed '/[A-Z][A-Z]*=/!d;s/=\(.*\)$/='"'"'\1'"'"'/;s/^\t//')
}

Last edited by jschiwal; 02-20-2009 at 03:37 PM.
 
Old 02-20-2009, 03:43 PM   #13
PatrickNew
Senior Member
 
Registered: Jan 2006
Location: Charleston, SC, USA
Distribution: Debian, Gentoo, Ubuntu, RHEL
Posts: 1,148
Blog Entries: 1

Rep: Reputation: 48
Quote:
Originally Posted by jschiwal View Post
If that is the actual form of the file you saved, the lines are also legal bash statements, and you can simply source it.

You are using a strange double quote. If you convert them to simple double quotes `"', then simply run:
. yourfile

After that, you can access the arrays normally.
. tempfile.sh
That is really cool. I'm adding that to the toolbox.
 
Old 02-20-2009, 09:04 PM   #14
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jschiwal View Post
If that is the actual form of the file you saved, the lines are also legal bash statements, and you can simply source it.

You are using a strange double quote. If you convert them to simple double quotes `"', then simply run:
. yourfile

After that, you can access the arrays normally.
. tempfile.sh
jschiwal@hpmedia:~> echo ${CDName[0]}
Please Please Me
jschiwal@hpmedia:~> echo ${CDName[1]}
Loving You

Here is an example where I used sed to use the NAME=VALUES results from ogginfo. I eval the results to set variables for a ogg2mp3 script.
Code:
cat bin/get_tags
get_tags()
{
    # clear out old tags
    ALBUM='';
    ARTIST='';
    DATE='';
    GENRE='';
    TITLE='';
    # the tags returned by ogginfo are of the form NAME=VALUE.
    # put single quotes around the values to encapsulate white space.
    # eval will set the tag variables ALBUM, ARTIST, DATE, GENRE, TITLE
    eval $(ogginfo ${1} | sed '/[A-Z][A-Z]*=/!d;s/=\(.*\)$/='"'"'\1'"'"'/;s/^\t//')
}
Thank you jschiwal for your lovely piece of code. The reasons why I had those strange double quotes is because I thought of simulating strings. Then I realized that it is already in a string so there is actually no need . Anyways, I'll work with your code and get back here when I finally get the script to work the way I want it to. Thank you again!
 
Old 03-21-2009, 02:45 PM   #15
nobody123
LQ Newbie
 
Registered: Feb 2009
Location: Toronto, ON
Distribution: Fedora 10
Posts: 16

Original Poster
Rep: Reputation: 0
*Update*

I just ended up creating a text file instead of using arrays. Makes it a whole lot easier to get values using grep. Srry for wasting everyone's time.
 
  


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
BASH: Reading long filenames into an array using a loop DaneM Programming 12 09-11-2009 07:24 AM
Java - reading from a file into an array joegumbo Programming 9 09-29-2008 05:12 PM
How to store text(strings) in a 2D character array reading from a text file(C++) bewidankit Programming 3 02-14-2008 07:08 AM
Reading crontab into bash array nutthick Programming 2 05-22-2006 12:44 PM
Reading from a txt file into a two dimension array in C kponenation Programming 3 11-26-2005 07:04 PM

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

All times are GMT -5. The time now is 10:08 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
Open Source Consulting | Domain Registration