Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

sed style file parse from command-line

by Anonymous Monk
on Dec 28, 2008 at 19:42 UTC ( #732916=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How do (can?) I read in a file and perform substitutions of this sort:
if ($_ =~ /^THIS/) {$_ =~ s/I/A/}
from the command line, rather than writing an actual script to open and rewrite the file.

Comment on sed style file parse from command-line
Download Code
Re: sed style file parse from command-line
by ccn (Vicar) on Dec 28, 2008 at 19:46 UTC
    $perl -pi'orig_*' -e 's/I/A/ if /^THIS/' fileA

    see: perldoc perlrun about -p, -i, -e switches

Re: sed style file parse from command-line
by Perlbotics (Abbot) on Dec 28, 2008 at 19:47 UTC

    Maybe something along:

    perl -pe ' /^THIS/ && s/I/A/ ' <infile.txt >outfile.txt
    ?

Re: sed style file parse from command-line
by Anonymous Monk on Dec 28, 2008 at 20:03 UTC
    Thanks much gang! Perlmonks rocks as always!
Re: sed style file parse from command-line
by superfrink (Curate) on Dec 28, 2008 at 23:45 UTC
    On a related note a couple days ago I used the following to delete lines that matched a pattern from a file.
    perl -n -i -e 'print unless (m/^.*line-to-remove$/)' fileA

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-12-22 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (126 votes), past polls