in reply to Re: sed regexp works on the command line, but not from Perl script
in thread sed regexp works on the command line, but not from Perl script
There is no need to "assume" what my code looks like, since I described it exactly. I'm sorry that troubled you. In any event, you did list it exactly as I described it.
When you say, "This is bad, of course...", it was not "of course" to me that it was bad, and if it were then I wouldn't have done it that way.
On checking whether filename.html is in the current directory, my full program first scans the directory for a list of files to work on, and then gets to work on each one, so I'm reasonably certain that the files are there, as no one else has access to the system besides me.
As for "I don't believe you that the inclusion of ^ was the only thing," I'm sorry that you think I am lying to you, but I am not.
When you said, "A saner solution would be to use system instead of backticks," I do not doubt you, but since I am a novice, I do not understand what is "insane" about using backticks.
I used the Tie::File suggestion and it works for me just fine. After reformatting was able to get it down to two lines, which I prefer. Thank you very much for that solution. I am still interested in getting the sed version to wor too, if for no other reason that on occasion I will need to process only one file and I am attracted to a one-line solution.
I don't think I was clear about my goal: It is to remove all leading spaces on *every line* of various files.
In the following examples, I collected the changed text into an array and printed the array so I didn't actually change the file, although in my final solution I will change it to actually write the file back to the disk.
This successfully removes all the spaces in the file, as a test, though that is not my ultimate goal:
@output = `sed 's/ //g' $filename`;
This removes the very first character, which is a space, but doesn't affect any other lines besides the very first line. This is a problem because I'm trying to process every line.
@output = `sed 's/^ //g' $filename`;
In my initial testing I did not realize that sed was indeed processing the first line, because the first character in the file used to be a nonspace, so I didn't see any change after I ran the script. I added an initial space to the file for testing.
The sed documentation seems to indicate that sed does not allow the "m" flag from Perl-style regexps which I understand to make the ^ operator work on every line, not just the first line. Then again, my understanding is that sed operates line-by-line by default, and indeed it works that way when I run it from the command line. However I can't get it to work properly when I call it from a Perl script using backticks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: sed regexp works on the command line, but not from Perl script
by Corion (Patriarch) on Dec 19, 2006 at 12:28 UTC |