0

I'm playing around with a script that updates a file replacing a line of text with one that is stored in a variable. After trying a whole bunch of things, I'm guessing the issue is with the text inserted, it's probably using characters that sed doesn't like. An example of how I'm using the command, with what the replacement text looks like is below:

sed 's/^define.*$/'define('AUTH_KEY',         'r*v8]Wic;@Y4{|0EQ9Z?~W,-P}k:d{k)ylAFHm-d(tY6v?U,5{hn].e9eH%/Xmdy');'/' change.html

I've read somewhere else on here that you need to escape the characters but was unable to get it working with the answer I found here: Escape a string for a sed replace pattern

Any help, or a pointer in the right direction is greatly appreciated, thank you! :)

Community
  • 1
  • 1
Smitty
  • 437
  • 2
  • 5
  • 8

2 Answers2

2

Be aware that sed can take any character as the separator, and that / is only conventional. If you can guarantee that your key is free of spaces, you could try:

sed 's ^define.*$ &(yourkey) '
thiton
  • 35,651
  • 4
  • 70
  • 100
  • I get a error when using the updated code, it says "syntax error near unexpected token ('". Perhaps I've mistyped? The code is: `sed 's/^define.*$ &'(define('AUTH_KEY','r*v8]Wic;@Y4{|0EQ9Z?~W,-P}k:d{k)ylAFHm-d(tY6v?U,5{hn].e9eH%/Xmdy');)''/' change.html` – Smitty Dec 29 '11 at 11:07
  • @Alen: Look more precisely at my post. And the ' token is special for the shell, you'll have to work around it. – thiton Dec 29 '11 at 11:15
0

Try something like this -

Assign your replacement text to a variable -

[jaypal:~/Temp] abc="'define('AUTH_KEY',         'r*v8]Wic;@Y4{|0EQ9Z?~W,-P}k:d{k)ylAFHm-d(tY6v?U,5{hn].e9eH%/Xmdy');'"

Use that variable in awk's sub function.

[jaypal:~/Temp] awk -v rep="$abc" '{sub(/^define.*$/,rep,$0); print}' change.html 
jaypal singh
  • 74,723
  • 23
  • 102
  • 147