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

437
Visualizações
Reformatting date times with awk

I have a file with rows like this:

2012/04/08 15:00,207
2012/04/08 16:00,180

I want to format the dates such that the file looks like:

08/04/2012 15:00,207

Awk would seem to be the right tool, but I can't get it to capture groups correctly. I've tried something like the following (Use sed or awk to fix date format):

awk ' BEGIN { FS = OFS = "," } { split($1, date, /\// ) $1 = date[3] "-" date[2] "-" date[1] " " date[4] print $0 } ' $FILE

But it leaves with me data like this:

08 15:00-04-2012,207

Awk's split is obviously interpreting the time as part of the year field of the date string, and I can't seem to find a way to split it off separately. I've tried a number of different things like colons or escaped spaces in the split delimiter field but all give syntax error. Have looked at awk split's documentation and was not enlightened by it.

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

0

$ awk '{split($1,date,"/"); print date[3] "/" date[2] "/" date[1], $2}' file
08/04/2012 15:00,207
08/04/2012 16:00,180

FYI there's alternatives with GNU awk or sed for capture groups:

$ awk '{print gensub(/(....)\/(..)\/(..)/,"\\3/\\2/\\1",1)}' file
08/04/2012 15:00,207
08/04/2012 16:00,180

$ awk 'match($0,/(....)\/(..)\/(..)(.*)/,a){ print a[3]"/"a[2]"/"a[1] a[4] }' file
08/04/2012 15:00,207
08/04/2012 16:00,180

$ sed -r 's#(....)/(..)/(..)#\3/\2/\1#' file
08/04/2012 15:00,207
08/04/2012 16:00,180
over 4 years ago · Santiago Trujillo Relatório

0

Here is a solution that uses the regex fieldsplitting by setting FS to a regex that splits your timestamp apart and then recombines it:

awk '
  BEGIN { FS= "[/ ]"; }
  {print $3 "/" $2 "/" $1 " "  $4 ;}
' file
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