简体   繁体   中英

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

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

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM