Command line pipe question

R

RyanY

Hello,

I am wondering if it is possible to pipe input to the -i command line
switch. For example:

perl -i -pe'orig_*' 's/string/newString/g' file1

will change all occurances of "string" with "newString" and create a
file called "orig_file1" that will be the original file. How do I pipe
the output of ls to this?

The following doesn't work.

ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'

I have read the perlfaq5 and understand how to do this within a perl
script. However, I came across the -i command line switch and wanted
to know if I could just pipe the filenames to it.

Thanks,
Ryan
 
J

John Bokma

RyanY said:
Hello,

I am wondering if it is possible to pipe input to the -i command line
switch. For example:

perl -i -pe'orig_*' 's/string/newString/g' file1

will change all occurances of "string" with "newString" and create a
file called "orig_file1" that will be the original file. How do I pipe
the output of ls to this?

The following doesn't work.

ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'

No, because -i means edit in place, so how do you want to edit the output
of ls in place :)

dir | perl -pe "s/a/b/g"

works here. (BTW, I don't understand your perl -i pe'orig_*' syntax, since
to me that would run 'orig_*' as perl code)
 
X

xhoster

RyanY said:
Hello,

I am wondering if it is possible to pipe input to the -i command line
switch. For example:

perl -i -pe'orig_*' 's/string/newString/g' file1

will change all occurances of "string" with "newString" and create a
file called "orig_file1" that will be the original file.

I'm pretty sure it won't. The argument to -i has to be immediately
after -i, with no intervening whitespace and certainly no intervening
switches.

How do I pipe
the output of ls to this?

The following doesn't work.

ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'

Use the shell.

perl -i'orig_*' -pe 's/string/newString/g' `ls *.txt`

Xho
 
J

John W. Krahn

RyanY said:
I am wondering if it is possible to pipe input to the -i command line
switch. For example:

perl -i -pe'orig_*' 's/string/newString/g' file1

will change all occurances of "string" with "newString" and create a
file called "orig_file1" that will be the original file.

No. That will run the expression 'orig_*' for every line in the two files
s/string/newString/g and file1 and edit them in-place without creating a
backup file.

How do I pipe the output of ls to this?

You don't really want to do that.

The following doesn't work.

ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'

perl -i'orig_*' -pe's/string/newString/g' *.txt



John
 
T

Tintin

I'm pretty sure it won't. The argument to -i has to be immediately
after -i, with no intervening whitespace and certainly no intervening
switches.



Use the shell.

perl -i'orig_*' -pe 's/string/newString/g' `ls *.txt`

UUOLS

perl -i'orig_*' -pe 's/string/newString/g' *.txt
 
X

xhoster

Tintin said:
UUOLS

perl -i'orig_*' -pe 's/string/newString/g' *.txt

I assumed the OP was following the guidelines by reducing his problem
to the simplest case that demonstrates what he wanted to demonstrate.
Doing that often results in constructs which are useless if you consider
the program to be an end-in-itself, but are not useless if you consider it
as what it is, a simplified demonstration.

Xho
 
P

Peter Scott

I am wondering if it is possible to pipe input to the -i command line
switch. For example:

perl -i -pe'orig_*' 's/string/newString/g' file1

will change all occurances of "string" with "newString" and create a
file called "orig_file1" that will be the original file.

No it won't; that's a syntax error. And the optional argument to the -i
flag is a suffix, not a prefix. perldoc perlrun.
How do I pipe the output of ls to this?

The following doesn't work.

ls *.txt|perl -i -pe'orig_*' 's/string/newString/g'

Use the for / foreach builtin of your shell, e.g.

foreach i (*.txt)
perl -i.orig -pe 's/string/newString/g' $i
end
 
J

John W. Krahn

Peter said:
No it won't; that's a syntax error. And the optional argument to the -i
flag is a suffix, not a prefix. perldoc perlrun.

Please read perlrun.pod more carefully:

perldoc perlrun
[snip]
This allows you to add a prefix to the backup file, instead of (or in
addition to) a suffix:

$ perl -pi'orig_*' -e 's/bar/baz/' fileA # backup to 'orig_fileA'


John
 
P

Peter Scott

Peter said:
No it won't; that's a syntax error. And the optional argument to the -i
flag is a suffix, not a prefix. perldoc perlrun.

Please read perlrun.pod more carefully:

perldoc perlrun
[snip]
This allows you to add a prefix to the backup file, instead of (or in
addition to) a suffix:

$ perl -pi'orig_*' -e 's/bar/baz/' fileA # backup to 'orig_fileA'

Good Lord, how did I miss that all this time? The OP's command line is
still a syntax error, though, because the argument to -i is in the wrong
place. Thanks for the eye-opener.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top