Pages

mardi 6 mars 2012

awk: NF; NR

1) get the last column (field) of the results of grep
grep ss filename | awk '{print $NF}' 

2) get the second last column:
grep ss filename | awk '{print $(NF-1)}' 

3) get the number of columns (if the array is uniform):

grep ss filename |  awk 'END {print NF}'

4) get the number of lines (if the array is uniform):
grep ss filename |  awk 'END {print NR}'

5) get the second last line:
grep ss filename |  awk 'END {print $(NR-1)}'

6) for an array in text file percentage_data, if we want to get the mean of each column:

# get the number of field in the array
set number_field = ` awk 'END{print NF}' percentage_data ` 

# for each column, find the mean and output the means in a new file 
# averaged_percentage_MII_MI

@ j = 1
while ( $j <= $number_field)
    set mean_percentage_temp = `awk -v fd=$j {print $fd}' percentage_data | awk '{sum+=$1}END{print sum/NR}'`
    echo $mean_percentage_temp " " >> averaged_percentage_MII_MI

    @ j += 1

end





Aucun commentaire:

Enregistrer un commentaire