You really should have tested it:
- -e should be immediatelly followed by the script to run,
- -p prints the current line, so you don't have to do it yourself, -n is what you want in this case.
This (tested!) script would work as:
perl -i.bak -n -e"print if(m/foo/);" foo.txt bar.txt baz.txt
From perldoc perlrun:
-n causes Perl to assume the following loop around your
program, which makes it iterate over filename
arguments somewhat like sed -n or awk:
LINE:
while (<>) {
... # your program goes here
}
"BEGIN" and "END" blocks may be used to capture
control before or after the implicit program loop,
just as in awk.
-p causes Perl to assume the following loop around your
program, which makes it iterate over filename
arguments somewhat like sed:
LINE:
while (<>) {
... # your program goes here
} continue {
print or die "-p destination: $!\n";
}
If a file named by an argument cannot be opened for
some reason, Perl warns you about it, and moves on to
the next file. Note that the lines are printed
automatically. An error occurring during printing is
treated as fatal. To suppress printing use the -n
switch. A -p overrides a -n switch.
"BEGIN" and "END" blocks may be used to capture
control before or after the implicit loop, just as in
awk.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|