I think there should be a way to do this simply, but I can't find it in the docs.
Syslog records contain literal dates that do not have leading zeros, but rather leading spaces. So Feb 1st is 'Feb..1' (2 spaces).
I want to remove one of those spaces (so I can 'split()' the line on spaces consistantly). Simple:
$a="test string x x\nFeb 1 09:12:33 (";
if ($a =~ /^\w{3}\s\s\d/m) { # /^Feb 1/
$a =~ s/^\w{3}(\s\s)\d/ /;
print "<$a>\n";
}else{
print "no match\n";
}
#<x x
Feb 1 09:12:33 (>
But it seems the substtitution isn't as smart as a regex. The program runs, but doesn't modify the string. It obviously recognizes the parens for grouping purposes, but only as insertions into the substituting text. But it can do:
my $var = 'testing';
$_ = 'In this string we are $var the "e" modifier.';
s/(\$\w+)/$1/ee;
print;
>In this string we are testing the "e" modifier.
It seem intuitive that a grouped substring would be the target of a substitution, if it exists for no other purpose. Or how about:
$a=~ s/$a =~ s/^\w{3}(\s\s)\d/$1/ /; #3 partr substitute like sed, $
+1 with a space?
So, is there a simple way to do substitution within a context?
It is always better to have seen your target for yourself, rather than depend upon someone else's description.
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.