Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Shortest way to replace a string

by nikfr (Initiate)
on Sep 11, 2013 at 22:58 UTC ( [id://1053590]=perlquestion: print w/replies, xml ) Need Help??

nikfr has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
i have a file containing strings like this:

30.00.02.53 some text here
54.00.70.13 some text here

30.00.02.36 some text here
54.00.70.13 some text here
70.00.00.23 some text here

30.00.11.05 some text here
30.00.00.00 some text here
30.00.00.13 some text here
54.00.70.13 some text here

and want to replace all what begins with 30.00 by 30.00.00.00
e.g. replace 30.00.02.53 by 30.00.00.00
What is the simplest (shortest) way to do this?

Thank you for your time!

Replies are listed 'Best First'.
Re: Shortest way to replace a string
by BrowserUk (Patriarch) on Sep 11, 2013 at 23:02 UTC

    perl -pE"s[30\.00\.\d\d\.\d\d][30.00.00.00]g;" infile > outfile

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Or
      s/(?<=30\.00\.)[0-9][0-9]\.[0-9][0-9]/00.00/g
      لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        The spec was "shortest" and that's 10 keystrokes longer; and certainly no more efficient :)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Shortest way to replace a string
by mcshell (Novice) on Sep 12, 2013 at 02:29 UTC
    s/^(\d+)(\.00)[^\s]+/$1$2$2$2/g;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-19 23:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found