4

I have a file name simcloud-target.txt where dates are saved in epoch time format i want to convert all the dates to human friendly format and print it as a list (same as the out put shown below so instead of epoch i want it in human friendly hh:mm:00)

sec504@slingshot:~$ cat simcloud-targets.txt
1679450009
1679450014
1679450018
1679450018
1679450022
1679450023
1679450024
1679450024
1679450033
1679450043
1679450046
1679450060
1679450065
1679450071
sec504@slingshot:~$ 

3 Answers 3

9

If the Unix timestamps in the file could be prefixed by @, then GNU date could read them in with -f. We may prefix each timestamp with @ using sed like so:

sed 's/^/@/' file

... and we may then use the output of that with GNU date like so:

date -f <(sed 's/^/@/' file)

Given the datestamps in question and my local timezone, this would give us

Wed Mar 22 02:53:29 CET 2023
Wed Mar 22 02:53:34 CET 2023
Wed Mar 22 02:53:38 CET 2023
Wed Mar 22 02:53:38 CET 2023
Wed Mar 22 02:53:42 CET 2023
Wed Mar 22 02:53:43 CET 2023
Wed Mar 22 02:53:44 CET 2023
Wed Mar 22 02:53:44 CET 2023
Wed Mar 22 02:53:53 CET 2023
Wed Mar 22 02:54:03 CET 2023
Wed Mar 22 02:54:06 CET 2023
Wed Mar 22 02:54:20 CET 2023
Wed Mar 22 02:54:25 CET 2023
Wed Mar 22 02:54:31 CET 2023

You may then set the output format you want as you would usually do with date. Here I also show how to use an ordinary pipe instead of a process substitution:

$ sed 's/^/@/' file | date -f - +'%F %T'
2023-03-22 02:53:29
2023-03-22 02:53:34
2023-03-22 02:53:38
2023-03-22 02:53:38
2023-03-22 02:53:42
2023-03-22 02:53:43
2023-03-22 02:53:44
2023-03-22 02:53:44
2023-03-22 02:53:53
2023-03-22 02:54:03
2023-03-22 02:54:06
2023-03-22 02:54:20
2023-03-22 02:54:25
2023-03-22 02:54:31

See the manual for the library function strftime (man 3 strftime) to see what format placeholders you may use.

5

Since you can use almost anything, you could use (g)awk:

awk '1{$1 = strftime("%F %H:%M:%S", $1); print}' < simcloud-targets.txt

yields (because I'm in a different time zone):

2023-03-22 02:53:29
2023-03-22 02:53:34
2023-03-22 02:53:38
2023-03-22 02:53:38
2023-03-22 02:53:42
…

I offer this, because it will still work even if you have other entries after the timestamp, such as in squid logfiles:

1679468468.282    223 redacted_ip TCP_TUNNEL/200 7160 CONNECT redacted_name:443 - HIER_DIRECT/redacted_ip -

becomes

2023-03-22 08:01:08 223 redacted_ip TCP_TUNNEL/200 7160 CONNECT redacted_name:443 - HIER_DIRECT/redacted_ip -
3

Like this, with xargs and GNU date:

$ xargs < file -I{} date -d '@{}' "+%F %T"

Or like this, with bourne* bash, dash, ash, yash, ksh, zsh, sh (but not compliant with csh & fish):

$ while read epoch; do date -d "@$epoch" "+%F %T"; done < file

Output

2023-03-22 08:53:29
2023-03-22 08:53:34
2023-03-22 08:53:38
2023-03-22 08:53:38
2023-03-22 08:53:42
2023-03-22 08:53:43
2023-03-22 08:53:44
2023-03-22 08:53:44
2023-03-22 08:53:53
2023-03-22 08:54:03
2023-03-22 08:54:06
2023-03-22 08:54:20
2023-03-22 08:54:25
2023-03-22 08:54:31

With :

$ perl -nE 'say scalar localtime($_)' file 
Wed Mar 22 08:53:29 2023
Wed Mar 22 08:53:34 2023
Wed Mar 22 08:53:38 2023
Wed Mar 22 08:53:38 2023
Wed Mar 22 08:53:42 2023
Wed Mar 22 08:53:43 2023
Wed Mar 22 08:53:44 2023
Wed Mar 22 08:53:44 2023
Wed Mar 22 08:53:53 2023
Wed Mar 22 08:54:03 2023
Wed Mar 22 08:54:06 2023
Wed Mar 22 08:54:20 2023
Wed Mar 22 08:54:25 2023
Wed Mar 22 08:54:31 2023
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.