- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Perl - String Subsitution problem
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 01:11 AM
тАО05-29-2009 01:11 AM
Perl - String Subsitution problem
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
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 02:23 AM
тАО05-29-2009 02:23 AM
Re: Perl - String Subsitution problem
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 03:05 AM
тАО05-29-2009 03:05 AM
Re: Perl - String Subsitution problem
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 03:51 AM
тАО05-29-2009 03:51 AM
Re: Perl - String Subsitution problem
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 04:10 AM
тАО05-29-2009 04:10 AM
Re: Perl - String Subsitution problem
Thanks a lot for the reply
:)
Priya
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 04:11 AM
тАО05-29-2009 04:11 AM
Re: Perl - String Subsitution problem
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО05-29-2009 10:53 PM
тАО05-29-2009 10:53 PM
Re: Perl - String Subsitution problem
($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