LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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
 
LinkBack Search this Thread
Old 11-10-2008, 12:20 PM   #1
Passions
LQ Newbie
 
Registered: Mar 2007
Distribution: RHEL 5.1
Posts: 20

Rep: Reputation: 0
Sed search for variable and delete entire line, but variable contains forward /'s


Hi, I wanted to search for a line and delete it, but the string contains a variable with various number of forward /'s.

path=/some/tmp/directory/that/is/different/each/time

sed "/$path/d" file

won't work. I tried replacing the forward slash, but I believe that's only when searching for strings.

sed "&$path&d" file
sed "%$path%d" file

doesn't work. Any ideas?
 
Old 11-10-2008, 12:49 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 13,837

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Quote:
Originally Posted by Passions View Post
Hi, I wanted to search for a line and delete it, but the string contains a variable with various number of forward /'s.

path=/some/tmp/directory/that/is/different/each/time

sed "/$path/d" file

won't work. I tried replacing the forward slash, but I believe that's only when searching for strings.

sed "&$path&d" file
sed "%$path%d" file

doesn't work. Any ideas?
Yep...precede the forward slash with a back-slash, like this:

sed "/\//d" file

should do it. The back-slash tells sed that the next thing is only a character, not any sort of 'special' thing (useful for things like $, ^, etc...)
 
Old 11-10-2008, 03:44 PM   #3
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 328

Rep: Reputation: 140Reputation: 140
If an alternative to the usual '/' delimiter is used in a sed line range the first one has to be escaped with a '\'. Try:
sed "\%$path%d" file
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
using sed to append a variable after a certain line jadeddog Programming 6 11-05-2008 12:49 AM
sed help with variable RudraB Programming 1 07-04-2008 01:10 AM
Pass a variable as a line number in sed dx0r515t Programming 7 03-30-2007 04:55 PM
sed and variable chirophil91 Programming 2 10-19-2004 03:23 AM
delete line specified by variable wihin shel script with sed lnchatterbox Linux - Newbie 2 02-23-2004 01:24 PM


All times are GMT -5. The time now is 03:12 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration