/e modifier to s///; short "1 liner" ?
/e modifier to s///; short "1 liner" ?
am 07.12.2007 19:26:23 von anonb6e9
Pls consider:
$ echo 'Dec 9'|perl -lpe 's/(Dec)(\s+)(\d+)/printf "$1$2 %d--huh?:",$3 + 5;/e;'
Dec 14--huh?:1
How do I get rid of the "1" in "--huh?:1". Also why is the "1" there?
If there is a cleaner approach to the above 1 liner, that would be fine
also - I just want preserve the rest of the input,
and add an integer offset to the date.
--
thanks much,
Tom
Re: /e modifier to s///; short "1 liner" ?
am 07.12.2007 20:55:01 von anonb6e9
In article <4759904e$0$11892$afc38c87@>,
Name withheld by request wrote:
>Pls consider:
>
> $ echo 'Dec 9'|perl -lpe 's/(Dec)(\s+)(\d+)/printf "$1$2 %d--huh?:",$3 + 5;/e;'
> Dec 14--huh?:1
>
>How do I get rid of the "1" in "--huh?:1". Also why is the "1" there?
still not sure we the "1" is coming from
>If there is a cleaner approach to the above 1 liner, that would be fine
>also - I just want preserve the rest of the input,
>and add an integer offset to the date.
I was not thinking about the -p switch very clearly...
This works for me:
$ echo 'a b c Dec 9 d e f'|perl -lne 's/^(.*?)(Dec)(\s+)(\d+)(.*)$/printf "$1$2$3 %d$5\n",$4 + 5/e;'
a b c Dec 14 d e f
Re: /e modifier to s///; short "1 liner" ?
am 07.12.2007 21:12:29 von xhoster
anonb6e9@nyx.net (Name withheld by request) wrote:
> In article <4759904e$0$11892$afc38c87@>,
> Name withheld by request wrote:
> >Pls consider:
> >
> > $ echo 'Dec 9'|perl -lpe 's/(Dec)(\s+)(\d+)/printf "$1$2 %d--huh?:",
> > $3 + 5;/e;'
> > Dec 14--huh?:1
> >
> >How do I get rid of the "1" in "--huh?:1". Also why is the "1" there?
>
> still not sure we the "1" is coming from
The evaluation of the printf has both a side-effect and a value.
The side effect is the printing of 'Dec 14--huh?:'. The return
value is "1", for success. The part of the thing in $_ that matched gets
replaced with this "1", because that is what s///e does. Due to the -p
switch, the value of $_ at the end of the implicit loop, namely "1", gets
printed.
> >If there is a cleaner approach to the above 1 liner, that would be fine
> >also - I just want preserve the rest of the input,
> >and add an integer offset to the date.
You should probably use sprintf. sprintf returns the formatted string,
and that return value would then get substituted into $_. printf doesn't
return the formatted string, it prints it to some filehandle and then
returns "1" upon success.
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
Re: /e modifier to s///; short "1 liner" ? <Thx for soln>
am 07.12.2007 21:35:08 von anonb6e9
--snip
>> > $ echo 'Dec 9'|perl -lpe 's/(Dec)(\s+)(\d+)/printf "$1$2 %d--huh?:",
>> > $3 + 5;/e;'
>> > Dec 14--huh?:1
>> >
>> >How do I get rid of the "1" in "--huh?:1". Also why is the "1" there?
>>
>> still not sure we the "1" is coming from
>
>The evaluation of the printf has both a side-effect and a value.
>The side effect is the printing of 'Dec 14--huh?:'. The return
>value is "1", for success. The part of the thing in $_ that matched gets
>replaced with this "1", because that is what s///e does. Due to the -p
>switch, the value of $_ at the end of the implicit loop, namely "1", gets
>printed.
Thanks for the detailed explanation.
--snip
>You should probably use sprintf. sprintf returns the formatted string,
>and that return value would then get substituted into $_. printf doesn't
>return the formatted string, it prints it to some filehandle and then
>returns "1" upon success.
Thanks again
Re: /e modifier to s///; short "1 liner" ? <test of soln, works fine>
am 07.12.2007 21:47:39 von anonb6e9
per your suggestion:
$ echo 'a b Dec 9 c d'|perl -lpe 's/(Dec)(\s+)(\d+)/sprintf "$1$2%d",$3 + 5/e;'
a b Dec 14 c d
works just fine
Re: /e modifier to s///; short "1 liner" ?
am 08.12.2007 00:21:48 von Glenn Jackman
At 2007-12-07 02:55PM, "Name withheld by request" wrote:
> >Pls consider:
> >
> > $ echo 'Dec 9'|perl -lpe 's/(Dec)(\s+)(\d+)/printf "$1$2 %d--huh?:",$3 + 5;/e;'
> > Dec 14--huh?:1
> >
> >How do I get rid of the "1" in "--huh?:1". Also why is the "1" there?
>
> >If there is a cleaner approach to the above 1 liner, that would be fine
> >also - I just want preserve the rest of the input,
> >and add an integer offset to the date.
Date calculations should be done with date modules. You probably don't
want to see "Dec 32".
echo 'Dec 9' | perl -MDate::Manip -lne '
print UnixDate(DateCalc(ParseDate($_),ParseDateDelta("+5d")), "%b %e")
'
Note I use -n instead of -p.
--
Glenn Jackman
"You can only be young once. But you can always be immature." -- Dave Barry