LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-01-2011, 01:55 PM   #1
seebee
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Rep: Reputation: Disabled
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"
 
Old 06-01-2011, 03:02 PM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by seebee View Post
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.

Last edited by MTK358; 06-01-2011 at 03:04 PM.
 
Old 06-01-2011, 03:17 PM   #3
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by MTK358 View Post
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
 
Old 06-01-2011, 03:22 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by crts View Post
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.
 
Old 06-07-2011, 03:58 PM   #5
seebee
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
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
 
Old 06-07-2011, 09:47 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,195

Rep: Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796Reputation: 1796
Please mark as SOLVED once you have a solution.
 
  


Reply

Tags
sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using sed to search and replace backwards jimieee Programming 15 10-25-2010 10:13 AM
sed - testing multiline search and replace webhope Programming 11 05-13-2010 02:21 PM
sed search replace tomerbd1 Linux - General 9 04-10-2008 04:31 AM
sed question for search and replace jakev383 Linux - General 8 05-05-2007 05:40 AM
sed search & replace sharathkv25 Programming 2 03-07-2007 10:16 AM


All times are GMT -5. The time now is 01:48 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration