LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-29-2012, 09:25 AM   #1
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Rep: Reputation: Disabled
SED with variable that contains spaces?


Im having problems using SED to replace with a variable. I assume the problem is that the variable contains spaces.

Code:
data=$(grep 'DATA' vsm.scr)
echo $data
sed "s/loc/$data/g" v1.scr > v2.scr
I just did the echo to make sure my variable is correct which it is. It has the format below:

DATA /home/temp/abc.txt


(There are additional spaces in front of DATA, they dont show up in the forum post)

Ive tried setting the variable as a simple number and everyhting works fine, which leads me to believe its a problem with the spaces

Any ideas?
 
Old 08-29-2012, 09:49 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,009

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Please provide an example of the line being replaced? As it stands i am unable to reproduce a problem.
 
Old 08-29-2012, 10:02 AM   #3
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
sed "s/loc/$data/g" v1.scr > v2.scr


loc is being replaced and is not a variable.
$data has the following format:
Code:
          DATA /home/temp/abc.txt
Note: there are ~10 spaces in the front (also $data is only one line its just showing up with an extra blank line after from the forum post)
 
Old 08-29-2012, 11:18 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
The problematic characters are not the spaces, since you correctly used double quotes to embed the sed command, but the slashes. They interfere with the separator used in the s command. To avoid this unwanted behaviour, use another character as separator, e.g.
Code:
sed "s:loc:$data:g" v1.scr > v2.scr
The general rule is that any character used immediately after the s command is treated as separator. You have only to choose a character that doesn't appear in any of the strings (a colon or a "@" are often a good choice).
 
2 members found this post helpful.
Old 08-29-2012, 11:25 AM   #5
mp85
LQ Newbie
 
Registered: Dec 2011
Posts: 18

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by colucix View Post
The problematic characters are not the spaces, since you correctly used double quotes to embed the sed command, but the slashes. They interfere with the separator used in the s command. To avoid this unwanted behaviour, use another character as separator, e.g.
Code:
sed "s:loc:$data:g" v1.scr > v2.scr
The general rule is that any character used immediately after the s command is treated as separator. You have only to choose a character that doesn't appear in any of the strings (a colon or a "@" are often a good choice).
Thanks so much. Worked perfectly!
 
  


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] SED - how to remove blank spaces carolflb Linux - Newbie 2 01-30-2010 06:02 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
Escaping spaces in a variable Ransak Programming 3 02-10-2008 01:54 AM
Inserting spaces using SED/AWK ? aravindts Programming 4 09-29-2006 05:28 AM
Problem with SED and blank spaces BigLarry Programming 2 06-10-2004 04:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:54 AM.

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