mojorising wrote:
Hello!
Hello,
I am trying to write a simple Perl script to edit a file using
two other source files named infra_east.txt and infra_west.txt. Please
excuse my ignorance on this. I am sure there are much better ways to
do what I am doing but this is the best I can come up with on my
currently limited knowledge. It's my first time dabbling with Perl.
When I put the following line in my command console I get this error:
Barewood found where operator expected at -e line 1, near "/s/emile"
(Missing operator before emile?)
In Perl a bareword is a string without quotation marks around it. /s/ is the
match operator with the pattern 's' which is matched against the current
contents of the $_ variable. So perl is telling you that you need to put an
operator (perldoc perlop) between /s/ and 'emile'.
syntax error at -e line 1, near "/s/emile"
Excecution of -e aborted due to compilation errors.
Here is the script:
perl -i.old -p -e /s/$(cat infra_east.txt)/$(cat infra_west.txt)/ig
configfile.cfg
A script would normally imply that you had written a Perl program and stored
it in an executable text file, what you have is usually called a "one-liner".
When you enter something on the command line the shell processes it first and
splits it on whitespace.
I basically copied an example from this page. It worked when I tested
it before. Not sure what's different. It appears exactly the same as
what's on that page and my test script.
http://open.itworld.com/nl/unix_sys_adm/09252002/
The examples from that page are:
perl -i -p -e s/floor/hamper/ig $file
perl -i -p -e 's/hamper = no/hamper = yes/' $file
perl -i -p -e 's/floor\s+yes/floor\tno/' $file
perl -i.bak -p -e s/floor/hamper/ig $file
In the second and third examples there are quotes around the string that the
-e switch executes as perl code. In the other two examples there are no
quotes but there are also no whitespace or shell meta-characters in the perl
code. Note also that there is no '/' character in front of the s///
substitution operator. If you use quotes then $(cat infra_east.txt) and $(cat
infra_west.txt) will not be iterpolated by the shell and perl will not
understand it correctly. If you don't use quotes then the shell will expand
$(cat infra_east.txt) and $(cat infra_west.txt) and they will probably contain
whitespace or regular expression meta-characters and then perl will not
understand it correctly.
This *may* work for you:
perl -i.old -pe"s/\Q$(cat infra_east.txt)/$(cat infra_west.txt)/ig"
configfile.cfg
If either infra_east.txt or infra_west.txt contains a '/' character it may not
work. What you should really do is:
perl -i.old -pe'BEGIN { $in = `cat infra_east.txt`; $out = `cat
infra_west.txt` } s/\Q$in/$out/ig' configfile.cfg
Can anyone give me a shove in the right direction? I have searched a
grat deal and haven't found anything explaining this. Oddly, a Google
search for this "barewood" error turns up no results. Weird.
Perl explains all its error and warning messages in the perldiag file, just
enter this from the command line:
perldoc perldiag
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/