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! :)

2 Answers 2

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) '
Sign up to request clarification or add additional context in comments.

2 Comments

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
@Alen: Look more precisely at my post. And the ' token is special for the shell, you'll have to work around it.
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 

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.