I'm using unix to read through an excel file, sort by one column, and then I want a percentage (lets say the top 10%) of the sorted lines exported to a new .xls file. I have the code below which works fine but I need to do a wc -l
prior to this line to get the "126", and then have to type it in (which is a hassle since I want to do successive filtering).
cat /Desktop/Results.xls | sort -n -k 3 | awk -F "[\t]" '{printf "%s\n",$0}' | head -$((126*1/10)) >./Results_0167_1.xls
You can modify the awk
to head
the file itself:
sort -n -k 3 /Desktop/Results.xls| awk '{a[i++]=$0}END{for (j=1;j<=i/10;j++){printf "%s\n",a[j]}}' >./Results_0167_1.xls
The awk
stores and counts ( a[i++]=$0
) the records piped by sort
, after this process , in the END
part it will print the recors till 10% is reached: j<=i/10