LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 01-22-2009, 11:15 AM   #1
FuzzieDice
Member
 
Registered: Oct 2004
Location: Rochester, NY
Distribution: Whatever Distro I need to work with.
Posts: 81
Thanked: 0
Question Regex: How do I put this on one line? (Time-date parsing)


[Log in to get rid of this advertisement]
I'm not great at regex so I'll try to explain what I have.

Code:
$line = "Log Created: [12/Dec/2008:06:35:15 -0800] by MyScript on 'ubuntu 8.04 LTS' - - Details: 1/2/3";

$stime = $& if $line =~ /\[.+\]/;
$stime =~ s/^\[//;
$stime =~ s/\]$//;
print "$stime\n";
Would like to put this into $stime in one line instead of 3 so that $stime would have "12/Dec/2008:06:35:15 -0800".

Someone know how to do this?
FuzzieDice is offline  
Tag This Post , , , ,
Reply With Quote
Old 01-22-2009, 11:23 AM   #2
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 685
Thanked: 47
If I understand you, and the lines look like your sample does, it's not too bad. Essentially you want to capture everything in between [ and ] and leave the time stuff in the order it's found. This works using Perl:
Code:
#!/usr/bin/perl
use strict;
use warnings;

my $line = "Log Created: [12/Dec/2008:06:35:15 -0800] by MyScript on 'ubuntu 8.04
LTS' - - Details: 1/2/3";

$line =~ m/\[(.+)\]/;
my $stime = $1;

print "$stime\n";
If you have multiple instances of [stuff] on any given line or stretching across lines, things get uglier. The parentheses () put the captured items into numbered scalars ($1, $2, etc.) for use later. Depending on how reliable your output is, it probably pays to check for the existence of $1 before you try to use it.
Code:
my $stime = $1 if $1;

Last edited by Telemachos; 01-22-2009 at 11:25 AM..
Telemachos is offline     Reply With Quote
Thanked by:
Old 01-22-2009, 11:56 AM   #3
FuzzieDice
Member
 
Registered: Oct 2004
Location: Rochester, NY
Distribution: Whatever Distro I need to work with.
Posts: 81
Thanked: 0

Original Poster
Thanks! This works.
FuzzieDice is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
how to convert windows date and time to unix date and time jitupatil_2007 General 8 03-31-2008 06:58 AM
Put a date/time from the command line Jzarecta Linux - Software 1 10-03-2007 01:48 PM
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 06:37 PM
Start Date + Time Duration = End Date/Time calculator? ToBe Linux - General 3 09-26-2005 11:17 AM


All times are GMT -5. The time now is 01:01 AM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration