- Community Home
- >
- Servers and Operating Systems
- >
- Operating System - HP-UX
- >
- Languages and Scripting
- >
- Perl - String Subsitution problem
-
-
Forums
- Products
- Servers and Operating Systems
- Storage
- Software
- Services
- HPE GreenLake
- Company
- Events
- Webinars
- Partner Solutions and Certifications
- Local Language
- China - 简体中文
- Japan - 日本語
- Korea - 한국어
- Taiwan - 繁體中文
-
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
- HPE Blog, Austria, Germany & Switzerland
- Blog HPE, France
- HPE Blog, Italy
- HPE Blog, Japan
- HPE Blog, Middle East
- HPE Blog, Latin America
- HPE Blog, Russia
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Blog, Poland
-
Blogs
- Advancing Life & Work
- Advantage EX
- Alliances
- Around the Storage Block
- HPE Blog, Latin America
- HPE Blog, Middle East
- HPE Blog, Saudi Arabia
- HPE Blog, South Africa
- HPE Blog, UK & Ireland
- HPE Ezmeral: Uncut
- OEM Solutions
- Servers & Systems: The Right Compute
- Tech Insights
- The Cloud Experience Everywhere
-
Information
- Community
- Welcome
- Getting Started
- FAQ
- Ranking Overview
- Rules of Participation
- Tips and Tricks
- Resources
- Announcements
- Email us
- Feedback
- Information Libraries
- Integrated Systems
- Networking
- Servers
- Storage
- Other HPE Sites
- Support Center
- Aruba Airheads Community
- Enterprise.nxt
- HPE Dev Community
- Cloud28+ Community
- Marketplace
-
Forums
-
Forums
-
Blogs
-
Information
-
English
- 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
Hewlett Packard Enterprise International
- Communities
- HPE Blogs and Forum
© Copyright 2022 Hewlett Packard Enterprise Development LP