|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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 |
| 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 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 |
|
|