Smitty
Smitty

Reputation: 467

Replace Line of Text Through Script

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

Upvotes: 0

Views: 186

Answers (2)

jaypal singh
jaypal singh

Reputation: 77075

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 

Upvotes: 0

thiton
thiton

Reputation: 36049

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) '

Upvotes: 2

Related Questions