LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Passing values from file into array in Bash (https://www.linuxquestions.org/questions/linux-newbie-8/passing-values-from-file-into-array-in-bash-4175437465/)

ShiGua 11-16-2012 01:27 PM

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!

TB0ne 11-16-2012 02:35 PM

Quote:

Originally Posted by ShiGua (Post 4830960)
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.

ShiGua 11-16-2012 03:01 PM

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

TB0ne 11-17-2012 02:47 PM

Quote:

Originally Posted by ShiGua (Post 4830994)
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.

catkin 11-17-2012 08:32 PM

Quote:

Originally Posted by ShiGua (Post 4830960)
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=( ... )


All times are GMT -5. The time now is 01:40 PM.