Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: replacing text in multiple files

by graff (Chancellor)
on Apr 14, 2012 at 02:49 UTC ( #965027=note: print w/ replies, xml ) Need Help??


in reply to replacing text in multiple files

If you have (a version of) the unix/linux "cat" utility, you can use the "diamond" operator as suggested by Eliya's initial reply, and pipe the concatenated set of files to your script's STDIN -- for that matter, you can also just print to STDOUT, and use redirection to a file on the command line:

cat *.log | your_script > modified.log-data
With that sort of usage, you don't need to worry about opening or closing files in your code; just read, do stuff, and print:
#!/usr/bin/perl use strict; while (<>) { # do stuff here, then... print; }


Comment on Re: replacing text in multiple files
Select or Download Code
Re^2: replacing text in multiple files
by Eliya (Vicar) on Apr 14, 2012 at 04:43 UTC
    cat *.log | your_script > modified.log-data

    Why use cat when Perl can do it all by itself?  With the added benefit of $ARGV letting you know which file you're working on, and being able to do in-place edits via -i / $^I:

    $ cat *.log bar1 bar2 bar3 baz1 baz2 baz3 foo1 foo2 foo3 $ perl -ne 'print "$ARGV: $_"' *.log bar.log: bar1 bar.log: bar2 bar.log: bar3 baz.log: baz1 baz.log: baz2 baz.log: baz3 foo.log: foo1 foo.log: foo2 foo.log: foo3 $ cat *.log | perl -ne 'print "$ARGV: $_"' -: bar1 -: bar2 -: bar3 -: baz1 -: baz2 -: baz3 -: foo1 -: foo2 -: foo3 $ perl -i -ne 'print ucfirst $_' *.log $ cat *.log Bar1 Bar2 Bar3 Baz1 Baz2 Baz3 Foo1 Foo2 Foo3

    (note to the OP:  -n is the short "command line" version of saying while (<>) { ... } — see perl -h )

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-11-26 20:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (173 votes), past polls