next up previous 405
Next: Selecting a range
Up: Reading tabular data
Previous: Finding the number of fields

Extracting columns

For a simple case without any comments.

     set col1 = `awk '{print $1}' fornax.dat`
Variable col1 is an array of the values of the first column. If you want an arbitrary column
     set col$j = `awk -v cn=$j '{print $cn}' fornax.dat`
where shell variable j is a positive integer and no more than the number of columns, returns the $j$th column in the shell array col$j$.

If there are comment lines to ignore, say beginning with # or *, the following excludes those from the array of values.

     set col$j = `awk -v cn=$j '$0!~/^[#*]/ {print $cn}' fornax.dat`
or you may want to exclude lines with alphabetic characters.
     set col2 = `awk '$0!~/[A-DF-Za-df-z]/ {print $2}' fornax.dat`
Notice that E and e are omitted to allow for exponents.



next up previous 405
Next: Selecting a range
Up: Reading tabular data
Previous: Finding the number of fields

C-shell Cookbook
Starlink Cookbook 4
Malcolm J. Currie
2006 November 26
E-mail:ussc@star.rl.ac.uk

Copyright © 2009 Science and Technology Facilities Council