NetSystemAdmin
NetSystemAdmin

Reputation: 545

Convert epoch columns to human readable format

I am using bash script to output data to a text file. The output file has first two columns that are in epoch format and I am trying to figure out how to add to the bash script to convert those columns into human readable format.

My script:

aws backup list-recovery-points-by-backup-vault --backup-vault-name "${VAULT_NAME}" --query "reverse(sort_by(RecoveryPoints, &RecoveryPointArn))[*].{RecoveryPoints: RecoveryPointArn, CreationDate: CreationDate, CompletionDate: CompletionDate}" --output table > getList.txt

Sample Output from getList.txt:

|  1607750797.22 |  1607749558.24 |  arn:aws:backup:us-west-1:010101010101:recovery-point:z0ab4c11-000d-4aaa-v01d-100z27zz32ab   |

|  1610688137.88 |  1610687158.08 |  arn:aws:backup:us-west-1:010101010101:recovery-point:a01q84c9-2035-1234-baf7-8aaa35aa111a   |

How can the output of the 1st and 2nd columns be converted into human readable format during exporting to getList.txt? Can I use something like following in the script:

awk 'BEGIN {FS=OFS="|"}{$1,$2=strftime("%c",$1, $2)} {print}'

Any help and suggestion is much appreciated.

Upvotes: 1

Views: 403

Answers (2)

ilkkachu
ilkkachu

Reputation: 6527

You'll have to do the columns one by one. Also note that since the lines start with a |, the first date is in field 2 (not 1).

$ awk 'BEGIN {FS=OFS="|";} 
         {$2 = strftime("%c", $2); $3 = strftime("%c", $3); } {print}' < foo.txt 
|Sat 12 Dec 2020 07:26:37 AM EET|Sat 12 Dec 2020 07:05:58 AM EET|  arn:aws:backup:us-west-1:010101010101:recovery-point:z0ab4c11-000d-4aaa-v01d-100z27zz32ab   |
|Fri 15 Jan 2021 07:22:17 AM EET|Fri 15 Jan 2021 07:05:58 AM EET|  arn:aws:backup:us-west-1:010101010101:recovery-point:a01q84c9-2035-1234-baf7-8aaa35aa111a   | 

Or something like this if you want to avoid repeating the strftime() call (it's not even shorter here, but anyway):

$ awk 'BEGIN {FS=OFS="|"; cols[2]=cols[3]=1} 
         { for (i in cols) $i = strftime("%c", $i) } {print}' < foo.txt 

Upvotes: 4

Setop
Setop

Reputation: 2490

In bash, you can call date commande:

~$ date -d @1607750979.22
Sat Dec 12 06:29:39 CET 2020

-d to specify a date instezd of now @epoch to input a date as epoch

Upvotes: 0

Related Questions