![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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* ![]() |
|
||||
|
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 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 |
|
||||
|
Gave that a try but didn't work. Thanks for the help though.
Quote:
Code:
echo "$VAR" | sed -e 's/\\/\\\\/g' -e 's/.*/s%&%%g/' | sed -f - file.txt >file_processed Code:
sed: newline or end of file found in pattern |
|
||||
|
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.
|
|
||||
|
Quote:
Thanks for the assistance, it does exactly what I want it to now. |
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|