1

I'm kinda new to Linux and Bash.

I'm trying to replace a line with another one and i don't know how to use sed right:

old line:

"certificate_path": "/etc/ajenti/ajenti.pem"

new line:

"certificate_path": "/etc/nginx/ssl/xyz-combined.pem"

I tried:

sed -i '26s/.*//etc/nginx/ssl/xyz.de-combined.pem"/' /etc/ajenti/config.json

best regards, Aeris

SLePort
  • 15,211
  • 3
  • 34
  • 44
Aeris
  • 307
  • 5
  • 16
  • There are SO many different things this question could mean. Split on `:`? Split after space? Search for certificate_path? Search for a string containing /s? Just about anything else? [edit] your question to state your requirements. – Ed Morton Jul 15 '16 at 09:13
  • Edit: Kenavoz's solution is working! – Aeris Jul 15 '16 at 09:45
  • A script that produces the output you expect from a small sample input is the starting point to identifying a solution, not the end point. I can see a few different ways the script you say "is working" will fail given different input values. See http://stackoverflow.com/q/29613304/1745001 for more info. – Ed Morton Jul 15 '16 at 17:04

4 Answers4

2

sed can use any character as separator for the substitution (s) command. In fact whatever follows s will be treated as a separator.

In your case, instead of escaping all slashes you can use different delimiter e.g. #.

 sed -i 's#"certificate_path": "/etc/ajenti/ajenti.pem"#"certificate_path": "/etc/nginx/ssl/xyz-combined.pem"#g' /etc/ajenti/config.json

Note: g option will replace all the occurrences of old pattern in a file. Remove it if you want to replace just first pattern.

ppp
  • 975
  • 7
  • 15
0

Using sed with backreference:

sed -i 's~\("certificate_path": "/etc/\)ajenti/ajenti.pem"~\1ssl/xyz-combined.pem"~' /etc/ajenti/config.json

The common part of old and new string is captured with brackets ("certificate_path": "/etc/) and prepended using backreference \1 to the new path : nginx/ssl/xyz-combined.pem".

Edit:

To use a variable in the replacement string, just surround the variable name with single quotes to allow parameter expansion:

mydomain="xyz-combined.pem"
sed -i 's~\("certificate_path": "/etc/\)ajenti/ajenti.pem"~\1ssl/'$mydomain'"~' /etc/ajenti/config.json

You can also surround the sed command with double quotes and escape all internal double quotes :

sed -i "s~\(\"certificate_path\": \"/etc/\)ajenti/ajenti.pem\"~\1ssl/$mydomain\"~" /etc/ajenti/config.json
SLePort
  • 15,211
  • 3
  • 34
  • 44
  • Wow thanks! That's exactly what I was looking for! Can i use a variable like: sed -i 's~\("certificate_path": "/etc/\)ajenti/ajenti.pem"~\1ssl/${MYDOMAIN}.pem"~' /etc/ajenti/config.json or do I have to modefy it? – Aeris Jul 15 '16 at 09:47
  • Thanks :) Youre awesome! – Aeris Jul 16 '16 at 21:07
0

Simply

sed -i 's@/etc/ajenti/ajenti.pem@/etc/nginx/ssl/xyz-combined.pem@g' \
   /etc/ajenti/config.json 

You don't exactly describe if there is anything else to replace.

Stefan Hegny
  • 2,107
  • 4
  • 23
  • 26
-2

Looking at your problem quickly, I guess you should be escaping the slashes by using a backslash before each slash.

Tell me if that worked.

Regards,