Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

again problem with regexpr

by white-fox (Novice)
on Mar 27, 2005 at 19:37 UTC ( #442686=perlquestion: print w/replies, xml ) Need Help??

white-fox has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: again problem with regexpr
by nerfherder (Monk) on Mar 27, 2005 at 20:37 UTC
    for example a text in this file like "trojan is like viruse" does exist..and i want to chage this like "hello". i know ,i know i can use regexpr s/// or use tr///...but how can i change it on my file...?
    Try using the substitution method (s///), and writing the output to a new file. You can then diff the new file with the original to verify your output.
    #!/usr/bin/perl my $infile = "/home/white_fox/filename.txt"; my $outfile = "/home/white_fox/filename-new.txt"; open (INFILEHANDLE, "< $infile") or die "Can't open input file because +: $! \n"; open (OUTFILEHANDLE, "< $outfile") or die "Can't open output file beca +use: $! \n"; while(<INFILEHANDLE>){ $_ =~ s/trojan is like viruse/hello/; $_ =~ s/phone \= 123456/5678/; print OUTFILEHANDLE $_; } close(INFILEHANDLE); close(OUTFILEHANDLE);

    Sounds like perhaps you're also looking for a regex to identify phone numbers. If that's the case, check out this node: http://www.perlmonks.org/index.pl?node_id=335348. Good luck!

Re: again problem with regexpr
by ambs (Pilgrim) on Mar 27, 2005 at 19:42 UTC
    You want to load a file from your hard dick? :-|

    What you want seems to be inplace editing:

    #!/usr/bin/perl -i.bak while(<>) { s/trojan is like viruse/hello/g; print }
    The -i switch makes your script to process the files you pass as arguments to the script inplace. The extra .bak makes it create a backup.

    Alberto Simões

Re: again problem with regexpr
by sh1tn (Priest) on Mar 27, 2005 at 20:43 UTC
    If you want this done through cli:
    perl -i.org -pe "s/phone = 123456/phone = 5678/ or s/trojan is like vi +ruse/hello/" data.txt
    where data.txt containts this:
    trojan is like viruse and / or phone = 123456


Re: again problem with regexpr
by manav (Scribe) on Mar 28, 2005 at 06:26 UTC
    #!/usr/bin/perl use strict ; use warnings ;
    standard program startup $^I=".bak" ; Enables in-place editing. I.e, Changes will be made to the same file for files read through <>
    @ARGV=(qw/abc.txt/) ;
    Makes <> read on abc.txt
    while(<>) { s/trojan is like viruse/hello/g; print ; }
    Replace "trojan is like viruse" with "hello". As in-place editing is enabled and print is not supplied an explicit filehandle, changes are made to the same file.


    Manav
Re: again problem with regexpr
by white-fox (Novice) on Mar 27, 2005 at 19:54 UTC
    no no....i mean....how do change in file....on context of file not how can load a file...no.... and ....
      Try explaining your question in your native language. Maybe someone here will be able to translate it.
      Can you give a more detailed example? (or wait and see if any other monk can get an idea of your problem)

      Alberto Simões

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2021-12-01 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    R or B?



    Results (11 votes). Check out past polls.

    Notices?