Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

433
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!