Monday 15 August 2011

bash - Reading delimited value from file into a array variable -


I want to read data.txt which has a 2x2 matrix number, is delimited by tab like this:

  0.5 0.1 0.3 0.2   

If this file is to be read in bash, then submit it to an array, then process a little bit and export the file again. Do you? For example in matlab:

  a = dlmread ('data.txt') // To read the file in the array variable i = 1: 2 for j = 1: 2 b [i ] [J] = one [i] [j] +100 end and dlmright (b, 'data2wt') // X-ray x-ray xrayentase value    

< If you have a limited range of processing like adding 100 in each entry, such a simple awk command can be:

/ P>

  awk '{for (i = 1; i & lt; = nf - 1; i ++) {printf ("% .ff% s", $ i + 100, OFS); } Printf ("% .1f% s", $ NF + 100, ORS); } '& Lt; Matrix.txt   

This is only the thief through each line and adds 100. It is also possible to do more complex operations, but better equipment (such as python numpy or octave ) if you really want the top rotation matrix. It is also possible to use bash arrays, but to do any, you have to use an external program, because bash floating point arithmetic Does not handle

No comments:

Post a Comment