Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
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
Replies are listed 'Best First'.
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 (Canon) 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 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
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!

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: (4)
As of 2015-08-01 01:54 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 (285 votes), past polls