Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

385
Visualizações
linux sort, select last line, pipe to new file

I have a series of multiple files. I would like to sort these files, select the bottom line from each, and pipe those lines into a new single file.

My files look like this:

1,  100,  2.5
2,  100,  3.3
3,  100,  5.1
4,  100,  1.2

These files are all named 51_Sur_extracted_data.csv

The files are housed in parent directories as follows:

Track_0001/output_dfsu/51_Sur_extracted_data.csv Track_0002/output_dfsu/51_Sur_extracted_data.csv

So I would like to sort all of the csv files on the 3rd column, extract the bottom line, and place it into a new summary file. Basically the goal is produce a file that has the maximum values from column three across all of the parent directories. Ideally, I would also like to add a column in the output file that contains the name of the parent directory (Track_0002).

I have the sort figured out, but the rest is proving a bit troublesome for me. For instance, can I sort all of the csv files at the same time and write the output to new files (example 51_Sur_extracted_data_sort.csv)? Then I can grep the last line and pipe it to a new file?

sort -t"," -k3,3g filename

Thanks, K

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

IF you want the last line of any given command, use tail.

So for you, you would do this:

sort -t"," -k3,3g filename | tail -n1 > newfilename
over 4 years ago · Santiago Trujillo Relatório

0

I'd do something like this:

for a in */*/*.csv
do 
    dname="$(basename "$(dirname "$a")")"
    echo -e "$dname\t$(sort -t"," -k3,3g "$a" | tail -n 1)"
done

On my test files it returned:

output_abcd 3,  100,  9.1
output_bcde 3,  100,  5.1
output_cdef 3,  100,  5.1
output_abcd 3,  100,  5.1
output_bcde 3,  100,  5.1
output_cdef 3,  100,  5.1
output_abcd 3,  100,  5.1
output_bcde 3,  100,  5.1
output_cdef 1,  100,  7.5
output_abcd 3,  100,  5.1
output_bcde 3,  100,  5.1
output_cdef 3,  100,  5.1
output_abcd 3,  100,  5.1
output_bcde 3,  100,  5.1
output_cdef 3,  100,  5.1
output_abcd 3,  100,  5.1
output_bcde 3,  100,  5.1
output_cdef 3,  100,  5.1
output_abcd 3,  100,  5.1
output_bcde 2,  100,  42.3
output_cdef 3,  100,  5.1
output_abcd 3,  100,  5.1
output_bcde 3,  100,  5.1
output_cdef 3,  100,  5.2
output_abcd 3,  100,  5.1
output_bcde 3,  100,  5.1
output_cdef 3,  100,  5.1

Yeah, my input data is a bit boring. Of course you can sort the end result again, but I'll leave that to your imagination (you already had that step) :)

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda