LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-22-2014, 04:57 PM   #1
bulldoggk
LQ Newbie
 
Registered: Apr 2014
Posts: 7

Rep: Reputation: Disabled
Help with sed syntax and single quotes


Hey all,

I've searched a ton on this, but couldn't find what I was hoping to find. I want to use sed to replace some text in a config file. I've used it successfully before in situations without single quotes, but this time single quotes are part of the string that I want to insert into the file.

Here's the command:

Code:
sed -i 's?library_dirs = [?library_dirs = ['$PATH_TO_FTDI', ?g' ./setup.py
What I want is to replace the text "library_dirs = [" in setup.py with "library_dirs = ['$PATH_TO_FTDI', " where the single quotes, comma, and space need to exist and $PATH_TO_FTDI will be replaced with what is stored in that environment variable.

I know the solution requires escaping, and I get that when it comes to, say, directory names with spaces in them (e.g. My\ Whacky\ Folder), but I'm not sure how to do it here.

Thanks,
Gabe
 
Old 04-22-2014, 05:11 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,

instead of escaping the single quotes in your replacement text you can switch to using double quotes in the sed command. You will however need to escape the $ and [. Eg
Code:
sed -i "s?library_dirs = \[?library_dirs = \['\$PATH_TO_FTDI', ?g" ./setup.py
HTH,

Evo2.
 
Old 04-22-2014, 05:28 PM   #3
bulldoggk
LQ Newbie
 
Registered: Apr 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
That worked almost perfectly, thanks!

The only thing it didn't do is replace $PATH_TO_FTDI with what is actually in that environment variable. PATH_TO_FTDI=/usr/local/include/libftdi1/, and it's that actual path that I want to show up in the setup.py file.

Unless python will see that environment variable as-is, which I guess would be ok too.
 
Old 04-22-2014, 06:30 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,753

Rep: Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288Reputation: 1288
Hi,
Quote:
Originally Posted by bulldoggk View Post
That worked almost perfectly, thanks!

The only thing it didn't do is replace $PATH_TO_FTDI with what is actually in that environment variable. PATH_TO_FTDI=/usr/local/include/libftdi1/, and it's that actual path that I want to show up in the setup.py file.
Ohh, I had assumed that was the desired effect. In this case don't escape the $. Ie do:
Code:
sed -i "s?library_dirs = \[?library_dirs = \['$PATH_TO_FTDI', ?g" ./setup.py
Quote:
Unless python will see that environment variable as-is, which I guess would be ok too.
No, to get to environment variables in python you need to use the getenv method of the os module. Eg
Code:
import os
print os.getenv('HOME')
Cheers,

Evo2.
 
Old 04-22-2014, 10:59 PM   #5
bulldoggk
LQ Newbie
 
Registered: Apr 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
Awesome, thanks!
 
Old 04-23-2014, 02:26 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
Remember to mark as SOLVED once you have a solution.
 
  


Reply


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to have a single quotes around a string sysmicuser Linux - Newbie 5 02-13-2013 11:04 PM
translate value from single quotes to double quotes venkateshrupineni Linux - Newbie 2 06-14-2012 04:03 PM
[SOLVED] How to escape single quotes in sed for multi line pattern XXLRay Linux - Software 1 07-18-2011 08:35 AM
Variables within single quotes are not evaluated? Akhran Linux - Newbie 1 03-10-2006 09:32 PM
Using single quotes vs double quotes in PHP strings vharishankar Programming 6 07-11-2005 12:41 PM


All times are GMT -5. The time now is 02:48 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration