Unix/Linux 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 Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 10:01 PM.. Reason: code tags
Sponsored Links
    #2  
Old Unix and Linux 11-16-2012
spacebar's Unix or Linux Image
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 71 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 Unix and Linux 11-19-2012
RudiC RudiC is offline Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 2 September 2015, 1:07 PM EDT
Location: Aachen, Germany
Posts: 6,622
Thanks: 113
Thanked 1,820 Times in 1,705 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 Unix and Linux 11-19-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 2 September 2015, 1:55 PM EDT
Location: Saskatchewan
Posts: 20,912
Thanks: 916
Thanked 3,736 Times in 3,488 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 04:52 PM
Greping array values in Bash like Perl paragkalra Shell Programming and Scripting 0 08-10-2010 02:38 PM
[bash] passing array to function ASGR Shell Programming and Scripting 4 10-23-2009 07:17 PM
[BASH - KSH] Passing array to a function ripat Shell Programming and Scripting 3 04-17-2008 08:17 AM
scripting: multiple values from file passing to command LisaS UNIX for Dummies Questions & Answers 5 07-20-2007 01:34 PM



All times are GMT -4. The time now is 02:57 PM.