Operating System - HP-UX
1752788 Members
5784 Online
108789 Solutions
New Discussion юеВ

Perl - String Subsitution problem

 
pgp_acc1
Advisor

Perl - String Subsitution problem

Hi,

I have a problem regarding date conversion actually. The date format will be in the following format : 2008/09/20-01-04-30.17.
I have to convert it to 2008092001043017.
Its working in the following format :
echo "2008/09/20-01-04-30.17" | perl -pi -e 's/\///g,s/-//g,s/\.//g'

But when i try the same like this:
perl -pi -e '
$k="2008/09/20-01-04-30.17";
$l= print $k; s/\///g,s/-//g,s/\.//g;
print $l; '

its not working, it gives the output like this : 20081

Thanks,
Priya
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: Perl - String Subsitution problem

Hi Priya:

There are several things here.

In your original post, the use of the '-i' switch is meaningless since there is no file to update. Otherwise the substitution is correct for removing the "/", "-" and ".".

In the second case (your question), you need to drop the '-p' switch since there is no input file to process. Drop the extraneous '-i' too. Now, to better see what's happening, add '-l' to automatically add a newline to print() statements:

# perl -le '$k="2008/09/20-01-04-30.17";$l=print $k;s/\///g,s/-//g,s/\.//g;print $l'
2008/09/20-01-04-30.17
1

Now, you see that the line with the "1" is from the scalar result of 'print $k' which is a true value since it isn't zero. A true value is one (1) and that's what is printed.

The substitution that you wrote operates on '$_' but that's not what you wanted. You should have done:

# perl -le '$k="2008/09/20-01-04-30.17";print $k;($l=$k)=~s/\///g;$l=~s/-//g;$l=~s/\.//g;print $l'

Lastly, if you find this helpful, please remember to evaluate the help you receive:

http://forums11.itrc.hp.com/service/forums/helptips.do?#28

You can appply that to your previous query too:

http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1313088

Doing so improves the quality of this community and helps its members find the most useful solutions.

Regards!

...JRF...
pgp_acc1
Advisor

Re: Perl - String Subsitution problem

Hi James,

Thanks for the detailed reply.
It really helped.
One more doubt is there any way to combine all three commands into one single one?
As this date change will have to be done most frequently.

Thanks,
Priya
James R. Ferguson
Acclaimed Contributor

Re: Perl - String Subsitution problem

Hi (again) Priya:

> One more doubt is there any way to combine all three commands into one single one?

Yes. You could use a bracket expresssion like:

# perl -le '$k="2008/09/20-01-04-30.17";print $k;($l=$k)=~s/[\/\.-]//g;print $l'
2008/09/20-01-04-30.17
2008092001043017

Notice that the "-" character is placed last in the bracket expression so that it isn't interpreted as a range. The "." doesn't need to be escaped within a bracket class, so we can keep it clean looking.

By the way, your original '$l= print $k' was simply capturing the return value of the print statement (not so meaningful, but one, or true, nevertheless). I misspoke about that before, not having had coffee, I mis-scanned :-).

Regards!

...JRF...
pgp_acc1
Advisor

Re: Perl - String Subsitution problem

James,

Thanks a lot for the reply
:)

Priya
pgp_acc1
Advisor

Re: Perl - String Subsitution problem

Got the solution from James
H.Merijn Brand (procura
Honored Contributor

Re: Perl - String Subsitution problem

If the original date format is always the same, and is assured to have leading zeroes, why not simply:

($l = $k) =~ s/\D//g;

As a side note, parsing dates and/or calculations with dates are usually better done with modules:

Date::Calc
Date::Parse
Date::Manip
DateTime

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn