LinuxQuestions.org
Visit Jeremy's Blog.
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
  Search this Thread
Old 01-25-2011, 01:02 PM   #1
wforumw
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Rep: Reputation: 0
bash sed question


Hi,
I have a variable like
Code:
var1="/home/"
and a file like

Code:
cat tmpfile
Quote:
12536412 /home/user1
856245 /home/user2
856332145 /home/user3
how can I remove the value of varable $var1 out of $tmpfile so that the end file looks like
Code:
cat tmpfile
Quote:
12536412 user1
856245 user2
856332145 user3
i tried
Code:
sed -e "s/$var1//g" tmpfile > new_tempfile
but I get
Quote:
sed: -e expression #1, char 9: unknown option to `s'
whe the variable a is wthout '/' than it works
how can I solve this
many thanks
 
Old 01-25-2011, 01:16 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Hi and welcome to LinuxQuestions!

Since the variable contains slashes, the resulting command after shell's variable substitution is
Code:
sed -e "s//home///g" tmpfile
which is obviously wrong. However sed let you choose the separator to avoid this kind of problems. The first character after the s command will be the separator, so that you can easily choose a character that never appears in the variable, e.g.
Code:
sed -e "s%$var1%%g" tmpfile
 
Old 01-25-2011, 01:54 PM   #3
wforumw
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Smile Solved

many thanks
 
  


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
sed question about bash related patolfo Programming 17 05-03-2010 10:57 AM
[SOLVED] BASH: sed question worm5252 Programming 5 03-26-2010 04:33 PM
Bash & sed question rengo.Java Programming 8 06-21-2007 10:59 PM
bash - sed question pk21 Programming 2 08-30-2003 06:57 AM
bash - sed question pk21 Programming 2 07-11-2003 04:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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