0

This command:

sed -i "\|^pref\.fullscreen\.toolbarPixels|d" ~/.vmware/preferences

Works.
But this command:

perl -pi -e "\|^pref\.fullscreen\.toolbarPixels|d" ~/.vmware/preferences

Give me the output:

Backslash found where operator expected at -e line 1, near "pref\"
Backslash found where operator expected at -e line 1, near "fullscreen\"
syntax error at -e line 1, near "\|"
Execution of -e aborted due to compilation errors.

Why?

Update:

with test.txt file that contains:

melon
banana
peach
apple

This command:

perl -ni -e "print unless m|apple|; print unless m|banana|" test.txt

Returns:

melon
melon
banana
peach
peach
apple

Instead of:

melon
peach

Why?

2 Answers 2

4

For the apple/banana problem, the duplicated items occur because both statements act on lines in the file. Since no statement filters melon or peach they are duplicated.

The following prevents the duplication and implements the wanted deletion:

$ perl -ni -e "print unless m/(apple|banana)/" test.txt

An alternative that moves the variability out of the regular expression:

$ perl -ni -e "print unless m/apple/ or m/banana/" test.txt
7
  • Thank you for second solution but steeldriver is unreachable. The "m" letter is unnecessary when the delimiter is standard, as sayd by steeldriver. In fact without "m" also works for me. Oct 30, 2020 at 16:46
  • perl -ni -e "print unless /apple|banana/" test.txt also works. Nov 2, 2020 at 14:59
  • 1
    I did not intend to minimize the expression but rather to minimize changes to what you posted in the question, and I do not understand the reason for fixating on whether "m" is allowed to be removed or not as this was not related to the problem being answered. I simply modified your expression enough to make it work. It is true that it is acceptable to drop the "m" from a match expression (only) when "/" is used as the delimiter. IMO, as I use many languages, it is easier to remember and use standard syntax notation regardless of delimiter rather than to focus on using syntactic exceptions.
    – kbulgrien
    Nov 2, 2020 at 15:53
  • 1
    Regarding the comment about syntactic exceptions, I don't intend to discourage you from dropping the "m". Some people might feel it overly verbose to use "m" in a statement where "m" is adjacent to an "=~" operator since some languages, like awk, use "~" to indicate a match expression, but, again, this had no bearing on question you asked. See Also 'if "/" is the delimiter then the initial m is optional' about half way down this manual page: perldoc.perl.org/perlop#Regexp-Quote-Like-Operators
    – kbulgrien
    Nov 2, 2020 at 16:19
  • 1
    This really is a separate question. Questions aren't really intended to be asked and answered in comments.
    – kbulgrien
    Nov 2, 2020 at 16:47
2

Perl isn't a drop-in replacement for sed - it happens to support a similar s/pattern/replacement/ syntax but that doesn't mean other sed commands will work.

You could instead do

perl -ni -e "print unless m|^pref\.fullscreen\.toolbarPixels|"

I think, or just

perl -ni -e "print unless /^pref\.fullscreen\.toolbarPixels/"

since there doesn't seem to be a particular reason to use a non-standard regex delimiter.

1
  • This command: perl -ni -e "print unless m|apple|; print unless m|banana|" test.txt don't give me the expected results. Why? Oct 28, 2020 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.