Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Replace an occurrence of a regexp with a function call on a substringof the match, multiple times on the line, multiple lines in a file

Reply
Thread Tools

Replace an occurrence of a regexp with a function call on a substringof the match, multiple times on the line, multiple lines in a file

 
 
David Karr
Guest
Posts: n/a
 
      09-16-2013
I have a pattern in an input file that looks like this:

latestTime="1379353492000"

I need to replace this with something like this:

latestTime="2013-09-16 10:34:33"

This pattern will occur in multiple lines in a file, and multiple times on a single line.

The hardest part of this seems to be to pass the milliseconds value into "strftime" as the replacement portion of the substitution regexp, for each occurrence. I can't get this to work.

This is what I have so far, and I hope I don't have to use that very strange syntax to call a function in the string (I found some references with examples like this):

#! /bin/perl
use English;
use strict;
use POSIX qw( strftime );

$| = 1;

while (my $line = <STDIN>) {
if ($line =~ /(^.*latestTime=")([0-9]+)(".*$)/) {
"\n";
$line =~ s/latestTime="([0-9]+)"/latestTime="${\strftime("%Y-%m-%d %H:%M:%S", localtime($1/1000))}"/g';
}
print $line;
}


 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-16-2013
David Karr <(E-Mail Removed)> writes:

> I have a pattern in an input file that looks like this:
>
> latestTime="1379353492000"
>
> I need to replace this with something like this:
>
> latestTime="2013-09-16 10:34:33"
>
> This pattern will occur in multiple lines in a file, and multiple
> times on a single line.
>
> The hardest part of this seems to be to pass the milliseconds value
> into "strftime" as the replacement portion of the substitution regexp,
> for each occurrence. I can't get this to work.
>
> This is what I have so far, and I hope I don't have to use that very
> strange syntax to call a function in the string (I found some
> references with examples like this):
>
> #! /bin/perl
> use English;
> use strict;
> use POSIX qw( strftime );
>
> $| = 1;


You probably don't need this (if you do, nothing in your problem
description suggest that you do). Also, since you "use English" it's
odd to then use the cryptic name for this variable. It's
$OUTPUT_AUTOFLUSH with "use English".

> while (my $line = <STDIN>) {
> if ($line =~ /(^.*latestTime=")([0-9]+)(".*$)/) {


You don't have to match the lines first -- just try the substitution you
want to do. Also, Perl has \d with is often better than writing [0-9].

> "\n";
> $line =~ s/latestTime="([0-9]+)"/latestTime="${\strftime("%Y-%m-%d %H:%M:%S", localtime($1/1000))}"/g';


You can use the "e" modifier to permit an expression in a substitution,
and you can put the whole text into strftime's format to get a simpler
expression:

$line =~ s!latestTime="(\d+)"!
strftime("latestTime=\"%Y-%m-%d %H:%M\"",
localtime($1/1000))!eg;

Because it's an expression, you can be free with the layout.

> }
> print $line;
> }


Finally, this pattern of looping and printing is so common that Perl
provides a command-line options for it. You can therefore write this
sort of thing as a single command, should that be more convenient than a
script:

perl -MPOSIX -pe 's/\d+/strftime("%F", localtime($1))/eg'

(using a simplified example).

--
Ben.
 
Reply With Quote
 
 
 
 
David Karr
Guest
Posts: n/a
 
      09-16-2013
On Monday, September 16, 2013 12:02:05 PM UTC-7, Ben Bacarisse wrote:
> David Karr <(E-Mail Removed)> writes:
>
>
>
> > I have a pattern in an input file that looks like this:

>
> >

>
> > latestTime="1379353492000"

>
> >

>
> > I need to replace this with something like this:

>
> >

>
> > latestTime="2013-09-16 10:34:33"

>
> >

>
> > This pattern will occur in multiple lines in a file, and multiple

>
> > times on a single line.

>
> >

>
> > The hardest part of this seems to be to pass the milliseconds value

>
> > into "strftime" as the replacement portion of the substitution regexp,

>
> > for each occurrence. I can't get this to work.

>
> >

>
> > This is what I have so far, and I hope I don't have to use that very

>
> > strange syntax to call a function in the string (I found some

>
> > references with examples like this):

>
> >

>
> > #! /bin/perl

>
> > use English;

>
> > use strict;

>
> > use POSIX qw( strftime );

>
> >

>
> > $| = 1;

>
>
>
> You probably don't need this (if you do, nothing in your problem
>
> description suggest that you do). Also, since you "use English" it's
>
> odd to then use the cryptic name for this variable. It's
>
> $OUTPUT_AUTOFLUSH with "use English".


Write Perl without cryptic syntax? What?

Thanks for the more expressive way to do this, and reminding me that I don't need it for this script.

> > while (my $line = <STDIN>) {

>
> > if ($line =~ /(^.*latestTime=")([0-9]+)(".*$)/) {

>
>
>
> You don't have to match the lines first -- just try the substitution you
>
> want to do. Also, Perl has \d with is often better than writing [0-9].
>
>
>
> > "\n";

>
> > $line =~ s/latestTime="([0-9]+)"/latestTime="${\strftime("%Y-%m-%d %H:%M:%S", localtime($1/1000))}"/g';

>
>
>
> You can use the "e" modifier to permit an expression in a substitution,
>
> and you can put the whole text into strftime's format to get a simpler
>
> expression:
>
>
>
> $line =~ s!latestTime="(\d+)"!
>
> strftime("latestTime=\"%Y-%m-%d %H:%M\"",
>
> localtime($1/1000))!eg;
>
>
>
> Because it's an expression, you can be free with the layout.


Outstanding. The "e" directive is definitely new to me. I'll have to read about that.

> > }

>
> > print $line;

>
> > }

>
>
>
> Finally, this pattern of looping and printing is so common that Perl
>
> provides a command-line options for it. You can therefore write this
>
> sort of thing as a single command, should that be more convenient than a
>
> script:
>
>
>
> perl -MPOSIX -pe 's/\d+/strftime("%F", localtime($1))/eg'


I'm writing this as a command-line filter tool, to be used with other filters, so I want the details in the script, not on the command line, but thanks for that option ayway.

> (using a simplified example).
>
>
>
> --
>
> Ben.


 
Reply With Quote
 
David Karr
Guest
Posts: n/a
 
      09-16-2013
On Monday, September 16, 2013 12:02:05 PM UTC-7, Ben Bacarisse wrote:
> David Karr <(E-Mail Removed)> writes:
> You can use the "e" modifier to permit an expression in a substitution,
>
> and you can put the whole text into strftime's format to get a simpler
>
> expression:
>
> $line =~ s!latestTime="(\d+)"!
> strftime("latestTime=\"%Y-%m-%d %H:%M\"",
> localtime($1/1000))!eg;
>
> Because it's an expression, you can be free with the layout.


I find this "e" directive curious. I can't find this documented anywhere. Is there a good reference for that?
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      09-16-2013
David Karr <(E-Mail Removed)> writes:

> On Monday, September 16, 2013 12:02:05 PM UTC-7, Ben Bacarisse wrote:
>> David Karr <(E-Mail Removed)> writes:
>> You can use the "e" modifier to permit an expression in a substitution,

<snip>
> I find this "e" directive curious. I can't find this documented
> anywhere. Is there a good reference for that?


It's documented in "perlop" as part of the documentation of s///.
Technically it's called a modifier or an option (the latter was a
surprise to me but it's one of the terms used in the documentation).

--
Ben.
 
Reply With Quote
 
 
 
Reply

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with the Java Access Bridge and the screenreader JAWS Markus Lemcke Java 3 05-07-2013 05:27 PM
Help with printing a bit pattern with printf and %x matt.jaffe@gmail.com C Programming 9 04-19-2013 12:14 PM
str.Template 4 times slower than calling replace multiple times Jack Steven Python 2 03-09-2009 05:38 AM
Want regex s/// to replace only nth occurrence jerrykrinock@gmail.com Perl Misc 12 07-09-2008 02:04 PM
Find first occurrence of string and replace Registered User C Programming 6 10-18-2006 10:28 PM



Advertisments