Com apenas awk
:
awk -F, 'BEGIN{PROCINFO["sorted_in"]="@ind_num_asc"} \
$1==1848||$2==1848 {a[$3]=$0} END {for(i in a) print a[i]}' file.csv
-
BEGIN{PROCINFO["sorted_in"]="@ind_num_asc"}
define a ordem de qualquer matriz que será criada de acordo com o índice, de acordo com os dígitos, em um estilo ascendente
-
$1==1848||$2==1848 {a[$3]=$0}
verifica se o primeiro ou o segundo campo é 1848, se assim for, então o terceiro campo ( $3
) é considerado como uma matriz a
index, com o valor sendo o registro inteiro ( $0
)
-
No END {for(i in a) print a[i]}
, é simples iterar as chaves e imprimir os valores
Para obter apenas os 5 registros, adicione head -5
no final:
awk ... | head -5
Apenas por questões de integridade, você pode obviamente obter apenas os 5 primeiros registros incorporando uma pequena lógica break
no loop END
, não há necessidade de tail
:
awk -F, 'BEGIN{PROCINFO["sorted_in"]="@ind_num_asc"} \
$1==1848||$2==1848 {a[$3]=$0} END {j=0; for(i in a) \
{print a[i]; j++; if(j==5) break}}' file.csv
Exemplo:
% cat file.txt
1848,2598,11.310694021273559
1848,2599,10.947275955606203
1848,2600,10.635270124233982
1848,2601,11.916564552040725
1848,2602,12.119810736845844
1848,2603,12.406661156256154
1848,2604,10.636275056472996
1848,2605,12.549890992708612
1848,2606,9.783802450936204
1848,2607,11.253697489670264
1848,2608,12.16385432290674
1848,2609,10.30355814063016
1848,2610,12.102525596913923
1848,2611,11.636595992818505
1848,2612,10.741178028606866
1848,2613,11.352414275107423
1848,2614,12.204860161717253
1848,2615,12.959915468475387
1848,2616,11.320652192610872
% awk -F, 'BEGIN{PROCINFO["sorted_in"]="@ind_num_asc"} $1==1848||$2==1848 {a[$3]=$0} END {for(i in a) print a[i]}' file.txt
1848,2606,9.783802450936204
1848,2609,10.30355814063016
1848,2600,10.635270124233982
1848,2604,10.636275056472996
1848,2612,10.741178028606866
1848,2599,10.947275955606203
1848,2607,11.253697489670264
1848,2598,11.310694021273559
1848,2616,11.320652192610872
1848,2613,11.352414275107423
1848,2611,11.636595992818505
1848,2601,11.916564552040725
1848,2610,12.102525596913923
1848,2602,12.119810736845844
1848,2608,12.16385432290674
1848,2614,12.204860161717253
1848,2603,12.406661156256154
1848,2605,12.549890992708612
1848,2615,12.959915468475387
% awk -F, 'BEGIN{PROCINFO["sorted_in"]="@ind_num_asc"} $1==1848||$2==1848 {a[$3]=$0} END {j=0; for(i in a) {print a[i]; j++; if(j==5) break}}' file.txt
1848,2606,9.783802450936204
1848,2609,10.30355814063016
1848,2600,10.635270124233982
1848,2604,10.636275056472996
1848,2612,10.741178028606866