Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to replace unixdate/time in a file with readable date 1

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello,

I have the following one liner that I use to convert unixdate to a more readable date. How do I replace the unixdate/time in the actual log file with the results of the converted time?

thanks for the help

Code:
for i in `cat trd_log.txt | grep -v LOG_FOR | awk '{print $1}' |cut -d '.' -f 1`; do date -d '1970-01-01 '$i' seconds'; done;
 
Hi

Please post some sample log lines too.

If happen to have [tt]gawk[/tt] that would simplify it : it could do the job of [tt]for[/tt], [tt]cat[/tt], [tt]grep[/tt], [tt]cut[/tt] and [tt]date[/tt] too.


Feherke.
 
Here are some sample logs, again I want to replace the unixdate/time with the human readable timestamp.

thanks again for the help!

Code:
1228998402.240819 2.2.2.2 1.1.1.1 RCV 323 0 0 0 23222 www
1229000232.644185 2.2.2.2 1.1.1.1 RST 445 0 0 0 34885 www
1229000232.667195 2.2.2.2 1.1.1.1 RST 994 0 0 0 44995 www
1229003736.784233 2.2.2.2 1.1.1.1 SYN 938 0 0 0 49449 www
1229003736.801099 2.2.2.2 1.1.1.1 RST 588 0 0 0  399399 www
 
Hi

Again, that is extremely easy if you have [tt]gawk[/tt] :
Code:
[blue]master #[/blue] awk -F. -vOFS=. '!/LOG_FOR/&&NF{$1=strftime("%c",$1)}1' trd_log.txt
Thu 11 Dec 2008 02:26:42 PM EET.240819 2.2.2.2 1.1.1.1 RCV 323 0 0 0 23222 www
Thu 11 Dec 2008 02:57:12 PM EET.644185 2.2.2.2 1.1.1.1 RST 445 0 0 0 34885 www
Thu 11 Dec 2008 02:57:12 PM EET.667195 2.2.2.2 1.1.1.1 RST 994 0 0 0 44995 www
Thu 11 Dec 2008 03:55:36 PM EET.784233 2.2.2.2 1.1.1.1 SYN 938 0 0 0 49449 www
Thu 11 Dec 2008 03:55:36 PM EET.801099 2.2.2.2 1.1.1.1 RST 588 0 0 0  399399 www

Feherke.
 
Hi

Or if you have [tt]bash[/tt] :
Code:
[blue]master #[/blue] IFS=.
[blue]master #[/blue] while read -a s; do
[blue]>[/blue]   [[ "${s[@]/LOG_FOR}" == "${s[@]}" && "${#s}" != 0 ]] && s[0]="$( date -d "1970-01-01 ${s[0]} seconds" )"
[blue]>[/blue]   echo "${s[*]}"
[blue]>[/blue] done < trd_log.txt
Thu Dec 11 12:26:42 EET 2008.240819 2.2.2.2 1.1.1.1 RCV 323 0 0 0 23222 www
Thu Dec 11 12:57:12 EET 2008.644185 2.2.2.2 1.1.1.1 RST 445 0 0 0 34885 www
Thu Dec 11 12:57:12 EET 2008.667195 2.2.2.2 1.1.1.1 RST 994 0 0 0 44995 www
Thu Dec 11 13:55:36 EET 2008.784233 2.2.2.2 1.1.1.1 SYN 938 0 0 0 49449 www
Thu Dec 11 13:55:36 EET 2008.801099 2.2.2.2 1.1.1.1 RST 588 0 0 0  399399 www

Feherke.
 
Hi

Or if you have [tt]ksh[/tt] this is almost the same :
Code:
while read f s; do
  [[ "${s/LOG_FOR}" == "${s}" && "${#s}" != 0 ]] && f="$( date -d "1970-01-01 $f seconds" )"
  echo "$f.$s"
done < trd_log.txt

Feherke.
 
Thanks for the replies, the only problem is that again I want to do an inplace edit of the file and replace the lines with the unixtime/date with the human readable date.

The code above does not do an in place edit.
 
Hi

Ok, learingperl01, then why you came to this forum and not use the language you have in your handle ?
Code:
perl -pa -i -F'\.' -e '$F[0]=localtime$F[0]if!/LOG_FOR/&&$#F;$_=join".",@F' trd_log.txt


Feherke.
 
Hi

Note that the above code only had to demonstrate how easy would be to change the [tt]gawk[/tt] syntax to [tt]perl[/tt]. But the in [tt]perl[/tt] it can be done more simple :
Code:
perl -pi -e 's/(\d+)/localtime$1/e if!/LOG_FOR/' trd_log.txt

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top