Pages

jeudi 16 février 2012

reading and processing lines in a text file

For a text file with arrays is called toto:


76039011873490122462 2592.624526   6407821.687553863   3 0.2461  0.0002892 9667 74    1  10 5320
76039011873490122462 2708.024084   6277975.529578525   3 0.2461  0.0002892 9667 74    2  10 5320
76039011873490122462 2835.623772   6179282.694306660   3 0.2461  0.0002892 9667 74    3  10 5320
76039011873490122462 2916.223642   6142426.064870278   3 0.2461  0.0002892 9667 74    4  10 5320
76039011873490122462 3055.424598   6126591.671642607   3 0.2461  0.0002892 9667 74    5  10 5320
76039011873490122462 3186.624722   6167350.779549360   3 0.2461  0.0002892 9667 74    6  10 5320

1) if we want to read the file:
set file = ` awk '{print $0}'  toto `

2) if we want to make some actions to each row, then

set num_lines =  `cat toto | wc -l`
@ j  = 1
while ( $j  <= $num_lines)
     set eachline =  `awk -v ln=$j   '{if (NR==ln) print $0}'  toto`
     more actions to eachline
     @ j += 1
end


Attention: with foreach, we can find each element in each row, but not to get a complete line !
foreach tt ( `cat toto `)
     set eachelement = $tt
end


3) to get a column of data, ie., column 4,

cat toto   |   awk  '{print $4}'

Aucun commentaire:

Enregistrer un commentaire