LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux Mint
User Name
Password
Linux Mint This forum is for the discussion of Linux Mint.

Notices


Reply
  Search this Thread
Old 07-22-2011, 01:08 PM   #1
linuxtuxian
LQ Newbie
 
Registered: Jul 2011
Posts: 2
Blog Entries: 2

Rep: Reputation: Disabled
Find and Replace


Hi I'm using shell script in which I declared a variable called $way as

way=`zenity --file-selection`

and need to use like following,

sed -ei "s/pathto/$way/" file > file

but I have a error

sed: -e expression #1, char 13: unknown option to `s'

how to use shell variable in sed? or is their any other way to replace a text using shell variable in a txt file?
 
Old 07-22-2011, 01:28 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,833
Blog Entries: 1

Rep: Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229Reputation: 1229
Hi and welcome to LQ.

I think the problem is when you actually put the path (eg. /home/user/etc/). It contains forward slashes (/).
You can either escape them with (\) or change the delimiter (/) to something else, eg.

Code:
sed -i "s|path/to/something|$way|" file
Pleas note that you don't need to redirect the output to another file when you use the -i flag (see the manual pages)

Additionally, it's recommended to use $(...) instead of backticks, as in:

Code:
way=$(zenity --file-selection)
hth

Last edited by sycamorex; 07-22-2011 at 02:35 PM. Reason: forgot the closing )
 
1 members found this post helpful.
Old 07-22-2011, 02:31 PM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Quote:
Originally Posted by sycamorex View Post
Additionally, it's recommended to use $(...) instead of backticks, as in:

Code:
way=$(zenity --file-selection
Also, you should put double quotes around $( ... ) so that it will work if the filename has spaces.

And what if the filename contains regular expression special characters? You will likely need to figure out a way to automatically escape them.
 
1 members found this post helpful.
  


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
can't find my find command! how to replace? dave247 Debian 4 11-19-2008 11:51 AM
Find and Replace dipuasks Linux - General 4 07-02-2008 02:52 PM
Find and Replace upwordz Linux - Newbie 7 05-10-2007 10:03 PM
find and replace happy78 Programming 11 09-10-2005 11:21 AM
Find and Replace? duerra Linux - General 9 01-28-2004 05:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux Mint

All times are GMT -5. The time now is 12:31 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