Reading columns from a text file and to make an array for each column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading columns from a text file and to make an array for each column
# 1  
Old 04-06-2013
Reading columns from a text file and to make an array for each column

Hi,
I am not so familiar with bash scripting and would appreciate your help here.
I have a text file 'input.txt' like this:
2 3 4
5 6 7
8 9 10
I want to store each column in an array like this
a ={2 5 8}, b={3 6 9}, c={4 7 10}
so that i can access any element, e.g b[1]=6 for the later use.
# 2  
Old 04-06-2013
Please use code tags as required by forum rules!

Unfortunately, you do not mention the shell that you are using. In bash, this should do:
Code:
$ while read X Y Z; do a[++i]=$X; b[i]=$Y; c[i]=$Z; done < file
$ echo ${a[@]} , ${b[@]} , ${c[@]}
2 5 8 , 3 6 9 , 4 7 10

If you need the indices start with 0, rearrange the assignments in the loop.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make copy of text file with columns removed (based on header)

Hello, I have some tab delimited text files with a three header rows. The headers look like, (sorry the tabs look so messy). index group Name input input input input input input input input input input input... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. UNIX for Dummies Questions & Answers

Solaris - Filter columns in text file and adding new column

Hello, I am very now to this, hope you can help, I am looking into editing a file in Solaris, with dinamic collums (lenght varies) and I need 2 things to be made, the fist is to filter the first column and third column from the file bellow file.txt, and create a new file with the 2 filtered... (8 Replies)
Discussion started by: jpbastos
8 Replies

3. Shell Programming and Scripting

Split text separated by ; in a column into multiple columns

Hi, I need help to split a long text in a column which is separated by ; and i need to print them out in multiple columns. My input file is tab-delimited and has 11 columns as below:- aRg02004 21452 asdfwf 21452 21452 4.6e-29 5e-29 -1 3 50 ffg|GGD|9009 14101.10 High class -node. ; ffg|GGD|969... (3 Replies)
Discussion started by: redse171
3 Replies

4. Shell Programming and Scripting

Reading a file into array

Hi, I need to read a file into array and print them in a loop:- 1st file :-cat a.txt RC1 RC2 RC3 RC4 My Program:- #!/bin/ksh index=0 while do read cnt<a.txt print "cnt value is ${cnt} index=`expr $index + 1` done Code tags for code, please. (5 Replies)
Discussion started by: satishmallidi
5 Replies

5. Shell Programming and Scripting

Help with reading column in array

I have some version problem to use this code in my server while read line; do read -A array <<<$line <---------- the server dont read <<< n=${#array} for ((i=1;i<$n;i++)); do print "${array}" done func=${array} data1=${array} data2=${array} ... (1 Reply)
Discussion started by: gavin_L
1 Replies

6. UNIX for Dummies Questions & Answers

How to insert alternative columns and sort text from first column to second?

Hi Everybody, I am just new to UNIX as well as to this forum. I have a text file with 10,000 coloumns and each coloumn contains values separated by space. I want to separate them into new coloumns..the file is something like this as ad af 1 A as ad af 1 D ... ... 1 and A are in one... (7 Replies)
Discussion started by: Unilearn
7 Replies

7. Emergency UNIX and Linux Support

awk- add columns and make new column and save as newfile

Hi, I have file as below: 5 6 7 4 8 9 3 5 6 output needs to be another file with 4th column as $1+$2 and 5th column as $3+$4. sample output file 5 6 7 11 18 4 8 9 12 21 3 5 6 8 14 Anybody have answer Thanks in advance (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

8. UNIX for Dummies Questions & Answers

How to make a loop base on reading a file?

To make it clearer: I have a file, List.txt List.txt contains: (these are actually splitted files w/c I got from ls command and dump them to the List.txt file) SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . . . And I want to rename these files to have a .dat extension.... (3 Replies)
Discussion started by: JohnBalayo
3 Replies

9. HP-UX

How to make a loop base on reading a file?

Need help on making a loop script base on what is inside a file... File to read: List.txt List.txt contains below w/c are file name as well: SAMPLEa SAMPLEb SAMPLEc SAMPLEd SAMPLEe SAMPLEf . . . Want to make a loop that will manipulate those that are inside the file.txt w/c are... (3 Replies)
Discussion started by: JohnBalayo
3 Replies

10. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies
Login or Register to Ask a Question