LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-26-2007, 07:30 PM   #1
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Rep: Reputation: 15
Perl question


I have a bunch of log files that begin with unix time and im trying to convert it all to human readable. Im trying to run a program that will look at my log files (data.txt) and take each line (i have thousands of lines) and convert the unix time into human readable. This means that i want the unix time to be taken out of the line and replaced with human readable. The below program is what i have so far. This is my first program so i apologize if my programing/documentation suck.



#!/usr/bin/perl
$file = '/data/data.txt';
$outputfl = '/data/output.txt';
open (INFO, $file);
$num=1;
open (OUTPUT, ">$outputfl");
#@lines = <INFO>;
#close (INFO);
#print @lines;
#while ($line = <INFO>)
while (<INFO>)
{
system ("\`echo OUTPUT\|cut -d' ' -f1"); --doesnt work
#this is how its done in bash -- `echo $line|cut -d' ' -f1
#I also need to run date --date=@$mydate +"%Y%m%d-%X %Z" and
#change the unix time to human readable
print system ("$_|cut -d'");
$num ++;
print "\n$num";
}
 
Old 12-26-2007, 08:13 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
show your sample log file and your expected output
 
Old 12-26-2007, 11:44 PM   #3
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Original Poster
Rep: Reputation: 15
Below are the logs im analyzing:

124376766.026 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
1124392388.975 RELEASE -1 FFFFFFFF 810FFBF233584C330353CF0A8C31F5D2 503 -1 -1 -1 unknown -1/813 POST http://log.cc.cometsystems.com/dss/cc.2_0_0.report_u

I run this command:
echo 124376766.026 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll|cut -d' ' -f1

and get

124376766.026

then i run

date --date=@124376766.026 +"%Y%m%d-%X %Z" --this and the other command are bash, i would appreciate if anyone could help me convert them to perl or should i just use system ()?

and get

19731210-06:06:06 AM MST


i want to take that output and put it back into my logs and replace the unix time so they look like this:

19731210-06:06:06 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll

Thank you,
 
Old 12-27-2007, 09:35 AM   #4
Hobbletoe
Member
 
Registered: Sep 2004
Location: Dayton, Oh
Distribution: Linux Mint 17
Posts: 150

Rep: Reputation: 18
Try this ...

Code:
#!/usr/bin/perl
${file} = '/data/data.txt';
${outputfl} = '/data/output.txt';
open (INFO, ${file});
open (OUTPUT, ">${outputfl}");
while (<INFO>)
{
 @{line_in}=split;
 @{line_in[0]}=`date --date=\@@{line_in[0]} +'%Y%m%d-%X %Z'`;
 chomp @{line_in[0]};
 print(OUTPUT join(' ', @{line_in}));
 print(OUTPUT "/n");
}
close INFO;
close OUTPUT;
I split the fields out into an array (by default, split assumes a space and the $_ variable. It could have been split(' ', $_) just as easily. Then, through my research this morning, system will only return the success/failure of the command (i.e., 1, 0, -1, 256 (error number)). You need to use the backticks to return the output. I had to chomp that ${line_in[0]} to get rid of the return. Then it was a simple matter of printing all back out.

As a note, you will see that I enclose all of my variables in braces {}. I do this so that I have better control of my variables, and know exactly where they end. I once tried to echo a variable with some letters directly after it. Say $abcd where only $a is my variable. Perl assumed that I wanted to use the entire thing as a variable. So, ${a}bcd corrected my issue for me, and I just stuck with it and use it everywhere.
 
Old 12-29-2007, 02:00 PM   #5
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Original Poster
Rep: Reputation: 15
I ran the program with the following input in data.txt

387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 810FFBF233584C330353CF0A8C31F5D2 503 -1 -1 -1 unknown -1/813 POST http://log.cc.cometsystems.com/dss/cc.2_0_0.report_u
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dllo
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
387702480 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
1113548880 RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://google.com



my output looks like this:

19820415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 810FFBF233584C330353CF0A8C31F5D2 503 -1 -1 -1 unknown -1/813 POST http://log.cc.cometsystems.com/dss/c...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...20415-12:08:00 AM MST RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/ho...50415-01:08:00 AM MDT RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://google.com/n

as you can see the first line looks good but every other line is not right. the date is inserted into the middle. Also the last date for google doesnt even show up. Any suggestions?

Thank you
 
Old 12-29-2007, 04:28 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,125

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Try "\n" in place of "/n"
 
Old 12-29-2007, 09:19 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Date conversion in Perl:
Code:
use Time::localtime;
$tm = localtime($time);
printf("Dateline: %02d:%02d:%02d-%04d/%02d/%02d\n",
    $tm->hour, $tm->min, $tm->sec, $tm->year+1900,
    $tm->mon+1, $tm->mday);
Hobbletoe: re "enclose all of my variables in braces {}.", please don't, it hurts the eyes and distracts the guy who comes after you (if you do this at work).
 
Old 12-29-2007, 09:29 PM   #8
dnoy
Member
 
Registered: Nov 2007
Posts: 69

Original Poster
Rep: Reputation: 15
the \n did the trick, i cant believe i missed that one

Thanks for everyones help
 
Old 01-04-2008, 02:25 PM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
or
Code:
use POSIX;
print strftime "%Y%m%d-%X %Z", localtime 124376766.026

19731210-13:06:06 GMT
 
Old 01-04-2008, 03:06 PM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
#!/usr/bin/perl -n
use POSIX;

($time, $line) = split " ", $_, 2;

print strftime "%Y%m%d-%X %Z $line", localtime $time;
Code:
[billy@daffy lxq]$ ./1.pl 1
19731210-13:06:06 GMT RELEASE -1 FFFFFFFF 4734C557F9315105CA6BE0FA56B94D55 200 1124276674 -1 -1 unknown -1/0 POST http://reports.hotbar.com/reports/hotbar/4.0/HbRpt.dll
20050818-20:13:08 BST RELEASE -1 FFFFFFFF 810FFBF233584C330353CF0A8C31F5D2 503 -1 -1 -1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Question for perl hackers. linuxlover1 Programming 1 06-27-2006 06:56 PM
reinstall perl question.. (urpme perl) rjcrews Mandriva 2 01-28-2006 06:19 PM
perl question nooodles Programming 5 09-22-2005 02:01 AM
Hiding code in PERL, perl gui question randomx Programming 1 06-26-2004 03:22 PM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration