Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

439
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda