Forum Moderators: coopster & phranque

Message Too Old, No Replies

Can't call sed successfully from Perl script

Works fine on the command line

         

MichaelBluejay

11:16 am on Dec 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This code works successfully from the command line:

sed 's/^ \{2,\}//' -i filename.html

But that exact same code doesn't run when it's in a Perl script. The file doesn't get modified. In the script, I enclose the code in backticks and add a semicolon at the end, that's the only difference. No error message, the script itself runs, but my file doesn't get changed.

I did some troubleshooting, removing the -i flag and assigning the output of sed to an array, and I found that the backticked sed commands worked fine on very simple pattern matches, but they failed when I either used the ^ operator to signal the start of a line, or the \{2,\} notation to specify 2 or more matches.

What am I doing wrong?

perl_diver

7:20 pm on Dec 17, 2006 (gmt 0)

10+ Year Member



comments removed, I did not read the question correctly so the answer was not relevant.

MichaelBluejay

7:26 pm on Dec 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Clearly sed can find the file, given that calling sed works fine as long as the regexp doesn't include ^ or \{2,\}.

I realize that I could manually open each file, read it into a variable, process it, and save it back to disk, but that seems cumbersome when I should be able to do it with one line with sed. The manual method also seems slower and a lot more memory intensive. I'll actually be processing thousands of files this way. But if you know of a more efficient way to do this from within Perl, I'm all ears.

perl_diver

1:26 am on Dec 18, 2006 (gmt 0)

10+ Year Member



You say "within" perl so I assume you are doing other things as well as what the sed command is doing. You can always use perls inplace editor, which is very fast. You can use it in a script or as a oneliner, this link should help you:

[perl.com...]

JerryOdom

1:36 am on Dec 18, 2006 (gmt 0)

10+ Year Member



If you're running the perl script via cron or some other automated method you have to give full paths to file locations otherwise it won't run properly

MichaelBluejay

3:22 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



JerryOdom, as I keep saying, my script can find the file just fine. That's not the problem.

MichaelBluejay

8:24 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



perl_diver, thanks for the link. But I only see from that article how to run a regexp from the command line. I don't see how to do it from within a Perl script -- unless I'm supposed to open the file, read the contents, run the regexp, write the results, and close the file -- which defeats the purpose of my trying to do the substitution in one line. If you know how to do it simpler than that, could you post the syntax?

perl_diver

7:34 pm on Dec 18, 2006 (gmt 0)

10+ Year Member



Sorry, I guess I got my reference materials confused. That link does not show how to use perls in place editor inside a script. This should help:



{
local $^I = '.bak'; # emulates -i.bak
local @ARGV = glob("*.html"); # get list of .html files
while (<>) {
s/^ {2,}//;
print;
} continue {close ARGV if eof}
}

MichaelBluejay

9:48 am on Dec 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thank you. I'm not sure that will work for me, though, because I'm using File::Find to recursively get a list of all files and process them one at a time, so I don't have any array of filenames. Further, I'm doing various tests on each file to decide whether I want to process it in the first place.

I guess my question should be: Given filename "$filename", what's the quickest/easiest way to run a search & replace on it from a Perl script?"

perl_diver

5:59 pm on Dec 19, 2006 (gmt 0)

10+ Year Member



you can incorporate my code into your code.

wruppert

6:07 am on Dec 20, 2006 (gmt 0)

10+ Year Member



You can use File::Slurp to efficiently read and write the entire file in one operation. You can then use normal Perl regular expressions to change the file.

theBear

4:05 pm on Jan 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To run any command from within perl you build the command in a string and pass that string to the system command.

IE:

$syscmd = "mv $filename2 $filename1";
system $syscmd;

will do a file move from within a perl script.

It should work with sed instead of mv.

Test it and let us know.