repcsi has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I came across this one liner, and I can't get it why it's not working: /usr/bin/perl -ni -e 'if ( $_ =~ /^#start /usr/sbin/xntpd/ ) { print "start /usr/sbin/xntpd \"$src_running\" \"-x\"\n" } else {print}' /etc/rc.tcpip This is what it should do: Check the lines in the file /etc/rc.tcpip and if it starts with "#start /usr/sbin/xntpd/" it should edit it to "start /usr/sbin/xntpd $src_running -x" with a new line char in the end. Error(so it looks like the syntax is wrong):
Bareword found where operator expected at -e line 1, near "/^#start /u +sr" (Missing operator before usr?) syntax error at -e line 1, near "/^#start /usr" Execution of -e aborted due to compilation errors.

Replies are listed 'Best First'.
Re: file line editor one-liner
by Anonymous Monk on Feb 10, 2012 at 10:34 UTC

    because the match operator, m//, uses matching delimiters, //, which need escaping, pretty much like you need to escape single quotes in 'single quoted strings', and double quotes in "double quoted strings"

    You can use balanced delimiters like m{} , and for substitution operator s{}{}

      Thank you, now it works! I had to escape $src_running with a backslash, because it was handled as a variable, but now it works! :) Working code:
      /usr/bin/perl -ni -e 'if ( $_ =~ m{^#start /usr/sbin/xntpd} ) { print +"start /usr/sbin/xntpd \"\$src_running\" \"-x\"\n" } else {print}' /e +tc/rc.tcpip