LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using sed - search and replace (https://www.linuxquestions.org/questions/programming-9/using-sed-search-and-replace-883989/)

seebee 06-01-2011 01:55 PM

Using sed - search and replace
 
I'm using sed in a bash script to search and replace and the correct syntax is alluding me.
Specifically I need the script to locate a line in a text file and replace the match. Below is my sample including the file to edit (source) and what I'd like the resulting file to look like. So far, I've not been able to expand ARG before writing it to the file.

thanks in advance - SeeBee

#!/bin/bash
ARGS='--channelfile /etc/mail/SA/sare-sa-update-channels.txt --gpgkey 856AA88A'

# Below line is the problem line.
sed -i 's/SAUPDATEARGS=""/SAUPDATEARGS="${ARGS}"/' update_sare

# eof

Source file: update_sare
#
# spamassassin settings
#
SAUPDATEARGS=""


Desired output: update_sare
#
# spamassassin settings
#
SAUPDATEARGS="--channelfile /etc/mail/SA/sare-sa-update-channels.txt --gpgkey 856AA88A"

MTK358 06-01-2011 03:02 PM

Quote:

Originally Posted by seebee (Post 4373359)
sed -i 's/SAUPDATEARGS=""/SAUPDATEARGS="${ARGS}"/' update_sare

Bash doesn't perform variable substitution in single quotes. Also, double quotes aren't special inside single quotes.

Code:

sed -i 's/SAUPDATEARGS=""/SAUPDATEARGS='"${ARGS}"'/' update_sare
Notice the single quotes around the double-quoted variable. The first single quote ends the single-quoted string, and the second one starts it again. And in bash, strings that are stuck together with no space in between are concatenated.

crts 06-01-2011 03:17 PM

Quote:

Originally Posted by MTK358 (Post 4373416)
Code:

sed -i 's/SAUPDATEARGS=""/SAUPDATEARGS='"${ARGS}"'/' update_sare

Almost there. Judging from the OP the replacement should also be enclosed in double-quotes.
Code:

sed -i "s/SAUPDATEARGS=\"\"/SAUPDATEARGS=\"${ARGS}\"/" update_sare
Of course you can also use the single-quote approach and have the double-quotes within the single-quotes, thus eliminating the need to escape them:
Code:

sed -i 's/SAUPDATEARGS=""/SAUPDATEARGS="'"${ARGS}"'"/' update_sare

MTK358 06-01-2011 03:22 PM

Quote:

Originally Posted by crts (Post 4373436)
Of course you can also use the single-quote approach and have the double-quotes within the single-quotes, thus eliminating the need to escape them:
Code:

sed -i 's/SAUPDATEARGS=""/SAUPDATEARGS="'"${ARGS}"'"/' update_sare

I would strongly recommend this, since in a real script, I would never put a regular expression in anything but single quotes.

seebee 06-07-2011 03:58 PM

thanks for the responses and the explanations. I tried options and kept at it. I finally figured out that the / in the ARGS was the issue and substituted the ^ symbol.

Works great now. thank you for the tips, they helped me get the solution!

ARGS="--channelfile /etc/mail/SA/sare-sa-update-channels.txt --gpgkey 856AA88A"

sed -i 's^SAUPDATEARGS=""^SAUPDATEARGS="'"${ARGS}"'"^' /tmp/update_sare

grail 06-07-2011 09:47 PM

Please mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 09:18 PM.