Skip to main content
added 200 characters in body
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29

A GNU awk alternative with use of date

awk -F'[/_]' '{ D=$(NF-3)"-"$(NF-2)"-"$(NF-1); "date +%Y-%m-%d -d "D|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f 2 

Walkthrough

Split out $0 your fields on / or _

awk -F'[/_]' '{ 

Recompose them as a valid date

 D=$(NF-3)"-"$(NF-2)"-"$(NF-1); 

Use the shell date function to convert the month from text to a number and grab it back by piping through awk's getline into a new variable

 "date +%Y-%m-%d -d "D|getline nd; 

Nothing new here

 print nd, $0 }' file1 | sort | cut -d" " -f 2 

Output

s3://xyz/private/backups/mails/daily/30_Mar_2020/ s3://xyz/private/backups/mails/daily/31_Mar_2020/ s3://xyz/private/backups/mails/daily/01_Apr_2020/ s3://xyz/private/backups/mails/daily/02_Apr_2020/ s3://xyz/private/backups/mails/daily/03_Apr_2020/ 

Alternative slightly leaner using gensub

awk -F'/' '{ "date +%Y%m%d -d "gensub("_","-","g",$(NF-1))|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f2 

A GNU awk alternative with use of date

awk -F'[/_]' '{ D=$(NF-3)"-"$(NF-2)"-"$(NF-1); "date +%Y-%m-%d -d "D|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f 2 

Walkthrough

Split out $0 your fields on / or _

awk -F'[/_]' '{ 

Recompose them as a valid date

 D=$(NF-3)"-"$(NF-2)"-"$(NF-1); 

Use the shell date function to convert the month from text to a number and grab it back by piping through awk's getline into a new variable

 "date +%Y-%m-%d -d "D|getline nd; 

Nothing new here

 print nd, $0 }' file1 | sort | cut -d" " -f 2 

Output

s3://xyz/private/backups/mails/daily/30_Mar_2020/ s3://xyz/private/backups/mails/daily/31_Mar_2020/ s3://xyz/private/backups/mails/daily/01_Apr_2020/ s3://xyz/private/backups/mails/daily/02_Apr_2020/ s3://xyz/private/backups/mails/daily/03_Apr_2020/ 

A GNU awk alternative with use of date

awk -F'[/_]' '{ D=$(NF-3)"-"$(NF-2)"-"$(NF-1); "date +%Y-%m-%d -d "D|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f 2 

Walkthrough

Split out $0 your fields on / or _

awk -F'[/_]' '{ 

Recompose them as a valid date

 D=$(NF-3)"-"$(NF-2)"-"$(NF-1); 

Use the shell date function to convert the month from text to a number and grab it back by piping through awk's getline into a new variable

 "date +%Y-%m-%d -d "D|getline nd; 

Nothing new here

 print nd, $0 }' file1 | sort | cut -d" " -f 2 

Output

s3://xyz/private/backups/mails/daily/30_Mar_2020/ s3://xyz/private/backups/mails/daily/31_Mar_2020/ s3://xyz/private/backups/mails/daily/01_Apr_2020/ s3://xyz/private/backups/mails/daily/02_Apr_2020/ s3://xyz/private/backups/mails/daily/03_Apr_2020/ 

Alternative slightly leaner using gensub

awk -F'/' '{ "date +%Y%m%d -d "gensub("_","-","g",$(NF-1))|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f2 
added 32 characters in body
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29

A GNU awk alternative with use of date

awk -F'[/_]' '{ D=$(NF-3)"-"$(NF-2)"-"$(NF-1); "date +%Y-%m-%d -d "D|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f 2 

Walkthrough

Split out $0 your fields on / or _

awk -F'[/_]' '{ 

Recompose them as a valid date

 D=$(NF-3)"-"$(NF-2)"-"$(NF-1); 

Use the shell date function to convert the month from text to a number and grab it back by piping through awk's getline into a new variable

 "date +%Y-%m-%d -d "D|getline nd; 

Nothing new here

 print nd, $0 }' file1 | sort | cut -d" " -f 2 

Output

s3://xyz/private/backups/mails/daily/30_Mar_2020/ s3://xyz/private/backups/mails/daily/31_Mar_2020/ s3://xyz/private/backups/mails/daily/01_Apr_2020/ s3://xyz/private/backups/mails/daily/02_Apr_2020/ s3://xyz/private/backups/mails/daily/03_Apr_2020/ 

A GNU awk alternative with use of date

awk -F'[/_]' '{ D=$(NF-3)"-"$(NF-2)"-"$(NF-1); "date +%Y-%m-%d -d "D|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f 2 

Walkthrough

Split out $0 your fields on / or _

awk -F'[/_]' '{ 

Recompose them as a valid date

 D=$(NF-3)"-"$(NF-2)"-"$(NF-1); 

Use the shell date function to convert and grab it back by piping through awk's getline into a new variable

 "date +%Y-%m-%d -d "D|getline nd; 

Nothing new here

 print nd, $0 }' file1 | sort | cut -d" " -f 2 

Output

s3://xyz/private/backups/mails/daily/30_Mar_2020/ s3://xyz/private/backups/mails/daily/31_Mar_2020/ s3://xyz/private/backups/mails/daily/01_Apr_2020/ s3://xyz/private/backups/mails/daily/02_Apr_2020/ s3://xyz/private/backups/mails/daily/03_Apr_2020/ 

A GNU awk alternative with use of date

awk -F'[/_]' '{ D=$(NF-3)"-"$(NF-2)"-"$(NF-1); "date +%Y-%m-%d -d "D|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f 2 

Walkthrough

Split out $0 your fields on / or _

awk -F'[/_]' '{ 

Recompose them as a valid date

 D=$(NF-3)"-"$(NF-2)"-"$(NF-1); 

Use the shell date function to convert the month from text to a number and grab it back by piping through awk's getline into a new variable

 "date +%Y-%m-%d -d "D|getline nd; 

Nothing new here

 print nd, $0 }' file1 | sort | cut -d" " -f 2 

Output

s3://xyz/private/backups/mails/daily/30_Mar_2020/ s3://xyz/private/backups/mails/daily/31_Mar_2020/ s3://xyz/private/backups/mails/daily/01_Apr_2020/ s3://xyz/private/backups/mails/daily/02_Apr_2020/ s3://xyz/private/backups/mails/daily/03_Apr_2020/ 
Source Link
bu5hman
  • 4.9k
  • 2
  • 16
  • 29

A GNU awk alternative with use of date

awk -F'[/_]' '{ D=$(NF-3)"-"$(NF-2)"-"$(NF-1); "date +%Y-%m-%d -d "D|getline nd; print nd, $0 }' file1 | sort | cut -d" " -f 2 

Walkthrough

Split out $0 your fields on / or _

awk -F'[/_]' '{ 

Recompose them as a valid date

 D=$(NF-3)"-"$(NF-2)"-"$(NF-1); 

Use the shell date function to convert and grab it back by piping through awk's getline into a new variable

 "date +%Y-%m-%d -d "D|getline nd; 

Nothing new here

 print nd, $0 }' file1 | sort | cut -d" " -f 2 

Output

s3://xyz/private/backups/mails/daily/30_Mar_2020/ s3://xyz/private/backups/mails/daily/31_Mar_2020/ s3://xyz/private/backups/mails/daily/01_Apr_2020/ s3://xyz/private/backups/mails/daily/02_Apr_2020/ s3://xyz/private/backups/mails/daily/03_Apr_2020/