Passing values from file into array in Bash | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Passing values from file into array in Bash

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-16-2012
ShiGua ShiGua is offline
Registered User
 
Join Date: Oct 2012
Last Activity: 16 December 2012, 1:42 PM EST
Posts: 15
Thanks: 6
Thanked 0 Times in 0 Posts
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":


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

and have arrays such as:


Code:
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:


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

Thank you!

Last edited by Scrutinizer; 11-16-2012 at 11:01 PM.. Reason: code tags
Sponsored Links
    #2  
Old 11-16-2012
spacebar's Avatar
spacebar spacebar is offline
Registered User
 
Join Date: Oct 2009
Last Activity: 2 June 2014, 3:23 PM EDT
Location: spaceBAR Central
Posts: 440
Thanks: 0
Thanked 70 Times in 70 Posts
Check out the below example, You should be able to modify it to do what you want to accomplish:

Code:
$ cat t
1 name1 a first
2 name2 b second
3 name3 c third


$ cat test.sh
# Script will load file lines into array(datafile) and then fields from each line into array(fields)
# and print each array(fields) element separating each element with the pipe symbol(|).
OLD_IFS=$IFS
IFS=$'\n'
datafile=( `cat "t"`)
IFS=$OLD_IFS
for l in "${datafile[@]}"
do
  fields=( $l );
  for f in $(seq 0 $((${#fields[@]} - 1)))
  do
    echo -n "${fields[$f]}"
    if (( f < $((${#fields[@]} - 1)) )); then
       echo -n "|"
    else
       echo ""
    fi
  done
done


$ test.sh
1|name1|a|first
2|name2|b|second
3|name3|c|third

Sponsored Links
    #3  
Old 11-19-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 23 November 2014, 12:04 PM EST
Location: Aachen, Germany
Posts: 4,687
Thanks: 78
Thanked 1,159 Times in 1,089 Posts
Try this; it works in my linux/bash system:
Code:
$ while read a; do eval row$((i++))="($a)"; done <file
$ for j in row{0..2}; do for ((i=0;i<4;i++)); do  eval col$i[${j/row/}]=\${$j[$i]} ;done; done
$ echo ${row0[@]}
1 name1 a first
$ echo ${row1[@]}
2 name2 b second
$ echo ${row2[@]}
3 name3 c third
$ echo ${col0[@]}
1 2 3
$ echo ${col1[@]}
name1 name2 name3
$ echo ${col2[@]}
a b c
$ echo ${col3[@]}
first second third

The Following User Says Thank You to RudiC For This Useful Post:
ShiGua (11-19-2012)
    #4  
Old 11-19-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 November 2014, 2:13 PM EST
Location: Saskatchewan
Posts: 19,909
Thanks: 845
Thanked 3,416 Times in 3,201 Posts
It would be far better to use this as a stream than to jam them all into shell variables. That is very inefficient and won't be portable.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Bash Passing An Array brsett Shell Programming and Scripting 2 08-24-2012 05:52 PM
Greping array values in Bash like Perl paragkalra Shell Programming and Scripting 0 08-10-2010 03:38 PM
[bash] passing array to function ASGR Shell Programming and Scripting 4 10-23-2009 08:17 PM
[BASH - KSH] Passing array to a function ripat Shell Programming and Scripting 3 04-17-2008 09:17 AM
scripting: multiple values from file passing to command LisaS UNIX for Dummies Questions & Answers 5 07-20-2007 02:34 PM



All times are GMT -4. The time now is 04:55 PM.