-3

In a file I have n number of epoch times.

I have to convert the each epoch time into dd/mm/yy format.

eg: file1 content:

1 aaa 1322625862.408 
2 bbb 1322625848.954
3 ccc 1322625843.908
4 ddd 1322625865.436

Now I want to convert this epoch time.

fedorqui
  • 275,237
  • 103
  • 548
  • 598

3 Answers3

1

you can use date:

reut@EliteBook-8470p:~/$ for epoch in $(cat f.txt | cut -d' ' -f 3); do date -d @$epoch +%d/%m/%y; done

Pipeline walkthourgh:

$(cat f.txt | cut -d' ' -f 3) takes each 3rd column in every line split by ' ' as dlimiter. This is the epoch string you have.

Now use each of these (using a for loop) as an argument to date -d @<eopch> and set the format to %d/%m/%y.

Output:

30/11/11
30/11/11
30/11/11
30/11/11

Edit: To actually store this in a result file:

while read serial name tdate; do echo ${serial} ${name} $(date -d@"${tdate}" "+%d/%m/%y") >> result_file; done < your_file.txt

Now your output is in result file.

Output in result_file:

1 aaa 30/11/11
2 bbb 30/11/11
3 ccc 30/11/11
4 ddd 30/11/11

Thanks @fedorqui.

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
1

It can be easier to just use read with dummy parameters and fetching just the ones you want:

while read _ _ epoch
do
   date -d@"$epoch" "+%d/%m/%y"
done < file

This way, the third word of every line gets stored into $epoch. Then, you use date -d@XXX to convert epoch to normal timestamp. Remember you can set a format using "+%Y%m..." and things like that.

Test

$ while read _ _ epoch; do date -d@$epoch "+%d/%m/%y"; done < a
30/11/11
30/11/11
30/11/11
30/11/11
fedorqui
  • 275,237
  • 103
  • 548
  • 598
0

You didn't specify but I believe you need Linux command. If you want to parse it from file just do:

for i in `awk '{print $2}' kuku `; do
date -d @$i +"%d/%m/%y"
done

it just takes from the file the relevant column with timestamp, and execute
date -d @ +"%d/%m/%y"

output:

30/11/11
30/11/11
30/11/11
30/11/11

Hope it help a bit

YyYo
  • 651
  • 5
  • 13