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

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; }

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://965027]
[LanX]: Choroba: this happened before I joined, was still in uni, but my boss was summoned to the CEO of the second biggest German bank at that time and could only say " I told them its not ready" ;)
[LanX]: memories....I missed my connection while chatting
[Discipulus]: in this case Corion we are speaking about software licensing: evry year or two we must rescan the whole ced to produce an excel report, while at every activation / disactivation we update a black box DB: i said that i a week i can produce the perl to..
[Discipulus]: rend out the xls IF i have access to the DB
[choroba]: LanX I miss working in a bank sometimes...
[Corion]: Discipulus: Ooof. Especially yearly things are things I like to automate instead of trying to remember how I did things last year...
[Corion]: And the second rule that I've learned is, that there is no one-off job, so writing a program for it pays off almost immediately. The third rule is to give all my programs numbers and have them reproduce that number in the name of their output files. :)
[Discipulus]: the true part is that also specification change between years.. but well our job is cheap but dont abuse of us.. ;=)

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (15)
As of 2017-03-29 12:07 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.