in reply to One liner: remove ssh keys with quotemeta

I don't use puppet either. However, I do not see the point in using regex for this:

/usr/bin/perl -ni -e 'print unless /^\\Q${line}\\E\$/' '${file}'

where it appears that you are just wanting an exact match on the entire line. While quotemeta will get you so far, remember that you are substituting ${line} in at the shell level, so any regex-terminating characters within that variable will also have to be escaped somehow. Much better to use an exact match I would have thought.

Similarly, you could specify the line to remove in an env var, thus avoiding one layer of escaping.

Without puppet and just using STDIN for clarity, here is a demo of both:

$ export LINE=bar $ echo -e "foo\nbar\nbaz" | perl -lne 'print unless $_ eq $ENV{LINE}'

You'll still need to sanitize/escape/quote your equivalent of "bar" when setting the environment variable but that should be a simpler task.

Replies are listed 'Best First'.
Re^2: One liner: remove ssh keys with quotemeta
by afoken (Chancellor) on Nov 28, 2019 at 12:51 UTC
    remember that you are substituting ${line} in at the shell level, so any regex-terminating characters within that variable will also have to be escaped somehow

    And here's my pet problem again: The problem of "the" default shell. It does not only affect perl, it is a general problem. And the solution is also generic - avoid the shell! From five minutes of Google, it seems that Puppet is written in Ruby. Ruby inherits many ideas from Perl, including a multi-argument form of exec(), including guesswork to avoid the shell when called with a single argument.

    So the sane way to get the job done is to use a multi-argument exec() already at the Puppet level, i.e. exec "perl","-E","say 'your code here'".

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)