The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Passing a variable to sed or cut gugs Shell Programming and Scripting 5 08-11-2008 03:27 AM
passing a variable inside a variable to a function KingVikram UNIX for Dummies Questions & Answers 2 01-14-2008 08:28 PM
passing value to a variable in a SQL kamitsin UNIX for Advanced & Expert Users 4 08-30-2007 03:21 PM
variable passing to sed Manish Jha Shell Programming and Scripting 2 08-18-2006 11:54 AM
Passing specific fields from files as variables keladar UNIX for Dummies Questions & Answers 4 04-13-2005 06:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-02-2008
Mr_Plow Mr_Plow is offline
Registered User
  
 

Join Date: Sep 2008
Location: Perth, Scotland
Posts: 8
Problem passing a specific variable to sed

Hi, I'm a bit of sed n00b here.

My issue is as follows:

I'm trying to pass a variable to sed so that all instances of this variable (in a text file) will be replaced with nothing. However, the value of this variable will always be a folder location e.g. "C:\Program Files\Folder1"

I currently have

sed "s/$VAR1//g" file.txt > file_processed.txt

Unfortunately this isn't working. I have also tried

sed s/$VAR1//g file.txt > file_processed.txt
sed 's/'$VAR1'//g file.txt > file_processed.txt

I am also using Korn Shell.

Please be gentle with your responses *puts on flame-proof jacket*
  #2 (permalink)  
Old 09-02-2008
dennis.jacob dennis.jacob is offline Forum Advisor  
dj -------
  
 

Join Date: Feb 2007
Location: Singapore/Bangalore/Cochin
Posts: 569
Try :

Code:
sed 's!'$VAR1'!!g' filename >file_processed
  #3 (permalink)  
Old 09-02-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
By "not working" I assume you mean the substitution doesn't come out the way you like it, because the backslashes are interpreted by sed, not used as literal characters in the substitution.

To solve that, you basically have to double every backslash in the substitution string before giving it to sed.

Code:
echo "$VAR" | sed -e 's/\\/\\\\/g' -e 's/.*/s%&%%g/' | sed -f - file.txt >file_processed
If your sed doesn't like getting a dash (standard input) as the argument to the -f option, some further workarounds are needed. Maybe you could simply use Perl instead /-:

Code:
perl -pe 'BEGIN { $v = quotemeta(shift); }
  s/$v//go' 'C:\Program Files\Folder1' file.txt >file_processed

Last edited by era; 09-02-2008 at 07:10 AM.. Reason: Add a Perl implementation
  #4 (permalink)  
Old 09-02-2008
Mr_Plow Mr_Plow is offline
Registered User
  
 

Join Date: Sep 2008
Location: Perth, Scotland
Posts: 8
Quote:
Originally Posted by dennis.jacob View Post
Try :

Code:
sed 's!'$VAR1'!!g' filename >file_processed
Gave that a try but didn't work. Thanks for the help though.

Quote:
Originally Posted by era View Post
By "not working" I assume you mean the substitution doesn't come out the way you like it, because the backslashes are interpreted by sed, not used as literal characters in the substitution.

To solve that, you basically have to double every backslash in the substitution string before giving it to sed.

Code:
echo "$VAR" | sed -e 's/\\/\\\\/g' -e 's/.*/s%&%%g/' | sed -f - file.txt >file_processed
If your sed doesn't like getting a dash (standard input) as the argument to the -f option, some further workarounds are needed. Maybe you could simply use Perl instead /-:

Code:
perl -pe 'BEGIN { $v = quotemeta(shift); }
  s/$v//go' 'C:\Program Files\Folder1' file.txt >file_processed
Tried
Code:
echo "$VAR" | sed -e 's/\\/\\\\/g' -e 's/.*/s%&%%g/' | sed -f - file.txt >file_processed
but got the error
Code:
sed: newline or end of file found in pattern
  #5 (permalink)  
Old 09-02-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The sed script works here, not sure how to fix it. Can you identify which part gives the error? (Try remove parts of the script starting from the end, until you no longer get the error message.) I'm guessing you might need to tweak the number of backslashes in s/\\/\\\\/g but that's a cheap shot.
  #6 (permalink)  
Old 09-02-2008
Mr_Plow Mr_Plow is offline
Registered User
  
 

Join Date: Sep 2008
Location: Perth, Scotland
Posts: 8
Quote:
Originally Posted by era View Post
The sed script works here, not sure how to fix it. Can you identify which part gives the error? (Try remove parts of the script starting from the end, until you no longer get the error message.) I'm guessing you might need to tweak the number of backslashes in s/\\/\\\\/g but that's a cheap shot.
I exited shell, and started it again and got it to work, not entirely sure why it didn't work before, but it does now!

Thanks for the assistance, it does exactly what I want it to now.
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 08:29 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0