LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 02-16-2011, 01:55 PM   #1
timdvtemp
Member
 
Registered: Feb 2011
Distribution: Redhat/Fedora
Posts: 39

Rep: Reputation: 3
Using Sed to search and replace does not accept spaces?


Hi all, I've been on linux just about as long as I can remember and viewed this forum plenty, but this is my first post!

Back to the problem: I wrote a little bash script that people around here can run to search and replace strings in the current directory and all subdirectories. However, whenever there is a space in the new string (the one doing the replacing), I get this error:

Sed: -e expression #1, char 9: unterminated `s' command

My code looks like this:

Code:
find ./ -type f -not -name find_SNR.sh -exec sed -i 's/'$FIND_SNR_STR1/$FIND_SNR_STR2/'' {} \;
Thanks!
 
Old 02-16-2011, 02:03 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Code:
's/'$FIND_SNR_STR1/$FIND_SNR_STR2/''
Because the s command IS unterminated. If you look at your command, you'll see:

's/'

whereas the syntax of s is 's/old/new/'

To use variables, please use double quotes (")
 
Old 02-16-2011, 02:31 PM   #3
timdvtemp
Member
 
Registered: Feb 2011
Distribution: Redhat/Fedora
Posts: 39

Original Poster
Rep: Reputation: 3
Thanks, but...

Ok, the spaces work now, but I want it to take the string "as it is". For example, if I wish to replace part of a path, the "/" will still mess everything up.

Code:
export FIND_SNR_STR1=$str1
export FIND_SNR_STR2=$str2
echo ""
find ./ -type f -not -name find_SNR.sh -exec sed -i "s/$FIND_SNR_STR1/$FIND_SNR_STR2/" {} \;
Thanks!
 
Old 02-16-2011, 02:34 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Use some other delimiter (make sure it's not in the search pattern), eg:

"s_$FIND1_FIND2_"

or

"s:$FIND1:$FIND2:"
 
Old 02-16-2011, 03:48 PM   #5
timdvtemp
Member
 
Registered: Feb 2011
Distribution: Redhat/Fedora
Posts: 39

Original Poster
Rep: Reputation: 3
works great, thanks!!
 
Old 02-16-2011, 04:19 PM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
You're welcome.

Here's a great tutorial on sed
http://www.grymoire.com/Unix/Sed.html

and btw, welcome to LQ
 
  


Reply



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



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 - Replace all spaces at beginning of line with the number 1. felix001 Programming 12 06-21-2010 12:22 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration