LinuxQuestions.org
Visit Jeremy's Blog.
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 11-16-2012, 01:27 PM   #1
ShiGua
LQ Newbie
 
Registered: Nov 2012
Posts: 5

Rep: Reputation: Disabled
Passing values from file into array in Bash


Hi,

I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column.

For example:

Sample file "file1.txt":

1 name1 a first
2 name2 b second
3 name3 c third

and have arrays such as:

line1 = ( "1" "name1" "a" "first" )
line2 = ( "2" "name2" "b" "second" )
line1 = ( "3" "name3" "c" "third" )

I'd also like to know if it's possible to pass each column of values into its own array, such as:

column1 = ( "1" "2" "3" )
column2 = ( "name1" "name2" "name3" )
column3 = ( "a" "b" "c" )
column4 = ( "first" "second" "third" )

Thank you!
 
Old 11-16-2012, 02:35 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by ShiGua View Post
Hi,
I'm trying to write a bash script that takes a file and passes each line from the file into an array with elements separated by column.

For example:
Sample file "file1.txt":

1 name1 a first
2 name2 b second
3 name3 c third

and have arrays such as:
line1 = ( "1" "name1" "a" "first" )
line2 = ( "2" "name2" "b" "second" )
line1 = ( "3" "name3" "c" "third" )

I'd also like to know if it's possible to pass each column of values into its own array, such as:
column1 = ( "1" "2" "3" )
column2 = ( "name1" "name2" "name3" )
column3 = ( "a" "b" "c" )
column4 = ( "first" "second" "third" )
Yes, all these things are possible. Post what you've written/tried so far, and tell us where you're stuck, and we'll be happy to help. However, this sounds very much like a homework question, and as a rule, you're not going to get someone here to do it for you.

If you show you've put some effort into things, and tell us where you're stuck, you're much more likely to get a favorable response.
 
Old 11-16-2012, 03:01 PM   #3
ShiGua
LQ Newbie
 
Registered: Nov 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks!

This isn't a homework question, I'm doing this for work and this is for the sake of assigning values from the file to arrays so I can more easily reference them later when I need to compare values with each other. Bash and perl are available to me, but I'm slightly more familiar with bash, so I'm starting there. I'm just trying to get this done as quickly as possible so we can move on to the next task. My main problem is not knowing the syntax, so I guess I could list my problem as a series of questions.

1. Assuming the file name is the first argument when running the program, $1 will reference the file name, I know that much. I'm assuming I need a loop of some kind to pass each line of the file into arrays so something along the lines of:

Code:
counter = 1
while [ $counter -le $(wc -l $1) ]; do
    read line$counter < #line $counter of $1
    let counter = counter + 1
end
How do I write that line "line $counter of $1", where it references line number "counter" from the file?

2. I have no idea how to pass the file values as columns, is there a specific command for this?

Thanks
 
Old 11-17-2012, 02:47 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,753

Rep: Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983Reputation: 7983
Quote:
Originally Posted by ShiGua View Post
Thanks!
This isn't a homework question, I'm doing this for work and this is for the sake of assigning values from the file to arrays so I can more easily reference them later when I need to compare values with each other. Bash and perl are available to me, but I'm slightly more familiar with bash, so I'm starting there. I'm just trying to get this done as quickly as possible so we can move on to the next task. My main problem is not knowing the syntax, so I guess I could list my problem as a series of questions.
Ok...again, as with your other thread, post what you've written/tried. And if it's not homework...it sure sounds like it. If you need it done quickly and its for work, why not let someone else at your company who knows scripting handle it?
Quote:
1. Assuming the file name is the first argument when running the program, $1 will reference the file name, I know that much. I'm assuming I need a loop of some kind to pass each line of the file into arrays so something along the lines of:
Code:
counter = 1
while [ $counter -le $(wc -l $1) ]; do
    read line$counter < #line $counter of $1
    let counter = counter + 1
end
How do I write that line "line $counter of $1", where it references line number "counter" from the file?
Putting "linux bash script loop through array" into Google pulls up this as the first hit, that tells you how, and has examples.
http://www.cyberciti.biz/faq/bash-for-loop-array/
Quote:
2. I have no idea how to pass the file values as columns, is there a specific command for this?
See previous example. Once you know how to define and read an array, you can then assign the column to an array, and pull out the elements.
 
Old 11-17-2012, 08:32 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ShiGua View Post
I'd also like to know if it's possible to pass each column of values into its own array, such as:

column1 = ( "1" "2" "3" )
column2 = ( "name1" "name2" "name3" )
column3 = ( "a" "b" "c" )
column4 = ( "first" "second" "third" )
The easiest way to find out would be to try it. BTW spaces are not allowed either side of the bash assignment operator, =, so the syntax is
Code:
array=( ... )
 
  


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
[SOLVED] reference bash array values, using a variable with a value of the array name gusthecat Programming 5 03-07-2012 03:41 PM
Bash + Reading values(numbers) from a file and storing them into an array SuchANewb Linux - Newbie 5 11-04-2010 08:11 AM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
array - passing values to sed command casperdaghost Linux - Newbie 3 08-21-2009 05:43 AM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM

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

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