Unix/Linux Go Back    


UNIX for Dummies Questions & Answers This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Passing values from file into array in Bash

UNIX for Dummies Questions & Answers


 
 
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 11: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: 21 September 2016, 12:03 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 online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 8 December 2016, 6:39 AM EST
Location: Aachen, Germany
Posts: 9,935
Thanks: 201
Thanked 2,953 Times in 2,754 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: 7 December 2016, 4:50 PM EST
Location: Saskatchewan
Posts: 21,664
Thanks: 1,010
Thanked 3,983 Times in 3,700 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
 

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 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 07:49 AM.