1

I'm trying to do a find/replace and an insert with sed but I keep getting a message that says "extra characters at the end of n command." I'm also not running this from the terminal, it's embedded in a shell script so I can send it to others.

This is what I'm trying to run

sed -i 's/include "/var/run/racoon/*.conf" ;/# include "/var/run/racoon/*.conf" ;/g' ~/Documents/test.conf;
sed '$a; include "/etc/racoon/remote/*.conf" ;/g' ~/Documents/test.conf;

To make it easier to see, I'm trying to replace

include "/var/run/racoon/*.conf" ;

with

# include "/var/run/racoon/*.conf" ;

and then add this

include "/etc/racoon/remote/*.conf" ;

Basically, I just want to comment out the last line of a file and then insert a line after it. I'm pretty new to sed so I'm not sure if I'm going about this all wrong, any help will be appreciated!

2 Answers 2

1

There are at least 2 problems with what you're trying:

  • You need to use different delimiters since your pattern and replacement contain /. You can use |.
  • The * in the pattern needs to be escaped, \*.

As such, the first expression would look like:

sed -i 's|include "/var/run/racoon/\*.conf" ;|# include "/var/run/racoon/*.conf" ;|g' ~/Documents/test.conf;

Similarly, change the second one.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I actually had tried that but it didn't work for me. Turns out my real problem was I didn't have gnu sed installed, which is what allows the -i option.
1

In case anybody runs into this problem, you either have to download gnu-sed or do it by other means.

You can download gnu-sed easily if you have Homebrew installed.

brew install gnu-sed

Or you can use perl and printf like I did

To do the replacement

sudo perl -pi -e 's|include "/var/run/racoon/\*.conf" ;|# include "/var/run/racoon/*.conf" ;|g' /etc/racoon/racoon.conf;

To add a line. It will always be line 139 for me so I could go this route.

line='include "/etc/racoon/remote/*.conf" ;'
sudo printf '%s\n' H 139i "$line" . wq | ed -s /etc/racoon/racoon.conf

Thank you to devnull for helping me get my syntax right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.