BASH-Constructing array from large numerical matrix .txt
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
BASH-Constructing array from large numerical matrix .txt
Okay, so very to bash scripting but comfortable using MATLAB. I have searched multiple forums and websites (with lots of experimenting) but can't quite find a working solution to my query, so I hope someone can point me in the right direction.
I have a txt file containing a nx3 matrix of data:
I want to read each line in bash, declaring each element as a separate variable.
e.g. For n=1: x=11, y=21, z=33
These variables are then used for a specific task which I will loop, changing the x,y,z variables at each iteration until the end of the file.
Current progress (limited):
ARRAY='cat artest.txt'
.... I get the whole matrix in the first element of the array, I can count the elements and also arrange into -L3 using xargs but cant access separate elements. What I think I need is to declare each element as an array and then loop updating..??
you mentioned in the thousands, so you might as well use something better, such as Perl, or Python ( and use their matrix/math libraries if possible). If you are restricted to only shell+shell tools, try using awk.
Code:
awk '{
print NR, $1,$2,$3
}' matrixfile
NR means the record number, $1 to $3 are your columns, ie, x, y, z in your example. please read more on awk (see my sig) to know how you can use it for your needs
Bash doesn't have two-dimensional arrays but you can simulate in this case by using index sets 11,12,13; 21,22,23; 31,32,33 etc.
Not tested, just for the ideas ...
Code:
#!/bin/bash
i=1
while read line
do
buf=( $line )
array[${i}1]=${buf[0]}
array[${i}2]=${buf[1]}
array[${i}3]=${buf[2]}
let i=i+1
done <<< "$( cat artest.txt )"
Last edited by catkin; 12-11-2009 at 04:41 AM.
Reason: Half the code jumped out of the code box -- all on its own :-)
you mentioned in the thousands, so you might as well use something better, such as Perl, or Python ( and use their matrix/math libraries if possible). If you are restricted to only shell+shell tools, try using awk.
Good point -- bash is not quick when handling a lot of data.
yup exec does things good as well esp. when reading input from coprocesses. you can also use 'read -u 4' to make that a bit cleaner. :-)
Quote:
Originally Posted by ghostdog74
..., try using awk.
Quote:
Originally Posted by catkin
Good point -- bash is not quick when handling a lot of data.
I agree as well. Awk is powerful when it comes to text and it can also do some math like creating hashes. Even a script compiler can be cleanly made with it. Base from the benchmarks I made with my scripts, it's really fast.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.