Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

So, I've been working on a simple program to help me parse some log files. I have a regular expression that searches each line of the file, if it matches my search string, then it returns the date and timestamp, as well as the result (numerical value) at the end of the log file line.

Here's a sample line from the file I'm parsing

20120704 00:05:53.46;CmdTask(0);EV;FBLdxPreAlignment rtn=0, Yoffset=-4278

my $tags[0] is set to "FBLdxPreAlignment"

Here's my regex

if ($_=~/(\d+ \d+:\d+:\d+.\d+).*$tags[0].*\=(\-?\d+.?\d*).*/){

When I just print the results, either to the console or to a file, it works perfectly.

print "$1,$2\n";

returns "20120704 00:05:53.46,-4278"

What I need, is for it to print the results, several commas, then a newline. This is so I can have separate columns of data in an output CSV for more than one search string at a time. When I change the code to print out a string of trailing commas, the following output happens.

print "$1,$2,,,,,\n";

returns ",,,,,704 00:05:53.46,-4278"

I'm not super experienced at regexs, so I thought maybe $1 $2 were somehow being modified on the fly. I tried saving them to a variable as soon as they were generated

my $data = "$1,$2\n"; chomp $data; print "$data\n"; print "$data,,,,,\n";

returns:

"20120704 00:05:53.46,-4278"

",,,,,704 00:05:53.46,-4278"

Now, you'll notice I added a newline and then chomped it. If I don't have the newline on the end, I get no output at all.

print "$1,$2";

Results: blank

The logfiles I'm working with are from a machine running VMS. I think the issues I'm having may be related to different formatting of the data (newlines characters or something). If I copy several lines from the file and save it as a .log file on my own computer, it works as expected (the comma's go at the end where I want them) I'll go ahead and post the entirety of my current code here at the end, in case there's some other error I've made that I'm missing.

foreach (@files){ $logfile=$_; print "$_\n"; print OUTFILE "Time,$tags[0],$tags[1],$tags[2],$tags[3],$tags[4]\n +"; # open LOGFILE, "$logfile"; while (<LOGFILE>){ chomp $_; if ($_=~/(\d+ \d+:\d+:\d+.\d+).*$tags[0].*\=(\-?\d+.?\d*).*/){ my $data = "$1,$2\n"; print "$data"; chomp $data; print "$data,,,,,\n"; print "$data"; } } close LOGFILE; }

In reply to Problems with formatting the results of my regex by superwombat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (5)
    As of 2017-12-16 11:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      What programming language do you hate the most?




















      Results (450 votes). Check out past polls.

      Notices?