Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Removing digits from a string

by suneel.reddy (Novice)
on Jan 01, 2013 at 10:22 UTC ( #1011135=perlquestion: print w/ replies, xml ) Need Help??
suneel.reddy has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I have a file name like - 20130101Customer100.imp , now I need to truncate 20130101 from the file name. I'm new to perl, can someone help me to solve this please ? I need only those first few digits to be removed (till an alphabet starts), not all the digits from the name.

Thanks in advance.

Comment on Removing digits from a string
Re: Removing digits from a string
by Anonymous Monk on Jan 01, 2013 at 10:53 UTC
    s/\d+//
      s/^\d+// # 'first few digits'
        s/^\D*\d+// # 'first few digits'
Re: Removing digits from a string
by johngg (Abbot) on Jan 01, 2013 at 12:39 UTC

    If that date format is consistent, use substr!

    $ perl -E ' > $filename = q{20130101Customer100.imp}; > say $filename; > substr $filename, 0, 8, q{}; > say $filename;' 20130101Customer100.imp Customer100.imp $

    Cheers,

    JohnGG

      Thank you John, thanks a lot...!!! yes - my filename will have a constant format. So,I hope substr works.

Re: Removing digits from a string
by blue_cowdawg (Monsignor) on Jan 01, 2013 at 16:20 UTC
        now I need to truncate 20130101 from the file name

    As someone else mentioned, I'm not sure the word "truncate" quite fits. Here's a solution that gives you a bit more flexibility:

    | hand waving here... my $fname = "20130101Customer100.imp"; # gotta go somewhere.. $fname =~ m@^(\d+)([A-Za-z0-9]+)\.imp$@; # Capture what we are looking + for... my $dateStamp=$1; my $basename=$2; | do something with this.
    This will result in the scalar $dateStamp containing "20130101" and $basename containing "Customer100". The presumption I made (you didn't specify otherwise) was that your filenames all have the extension "imp" and as long as they do the regex I supplied you will work. I leave it as an exercise in intellect for you to modify the regex if there are other extensions involved.


    Peter L. Berghold -- Unix Professional
    Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg
Re: Removing digits from a string
by suneel.reddy (Novice) on Jan 02, 2013 at 09:03 UTC

    Hi Monks, Thank you all for spending some quality time to solve my issue. I see two/three sollutions will definitely solve my problem, also surprised to see you guys gave some extra analysis which was interesting. Sorry I din't mention that - my filename will have the same filename format everytime. Thanks again.......!!!!!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1011135]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (14)
As of 2015-07-07 14:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (89 votes), past polls