Problem passing a specific variable to sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem passing a specific variable to sed
# 1  
Old 09-02-2008
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* Smilie
# 2  
Old 09-02-2008
Try :

Code:
sed 's!'$VAR1'!!g' filename >file_processed

# 3  
Old 09-02-2008
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 08:10 AM.. Reason: Add a Perl implementation
# 4  
Old 09-02-2008
Quote:
Originally Posted by dennis.jacob
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
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  
Old 09-02-2008
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  
Old 09-02-2008
Quote:
Originally Posted by era
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. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing specific and incrementing lines of text from file via variable

This is part of a larger script where I need to pass only 1 line of a file to the script, based on a variable and not a direct reference. As part of a for loop : # for((line=0;line<50;line++)); do # awk ‘NR==$line' PhraseList.txt; done ... (5 Replies)
Discussion started by: Seth
5 Replies

2. UNIX for Dummies Questions & Answers

Passing value of a variable in sed

Hi, I want to pass value of a variable track_line which is the line number to sed. Sed should print the lines starting from track_line till the last line of the file. I tried the below command but it is not working. sed -n '${track_line},$p' latest_log_file I tried using the below too but... (1 Reply)
Discussion started by: nitinupadhyaya8
1 Replies

3. Shell Programming and Scripting

Trouble with passing variable to sed

Here is my code #!/bin/bash username=gnowicki sed '$s/$/ $username/' < sshd_config 1 <> sshd_config what this is supposed to do is take the name gnowicki and put it at the end of the last line of the sshd_config and it works except not using the variable, if I put the name "gnowicki" where... (2 Replies)
Discussion started by: slufoot80
2 Replies

4. Shell Programming and Scripting

Passing parameter in sed or awk commands to print for the specific line in a file

Hi, I am trying to print a specific line in a file through sed or awk. The line number will be passed as a parameter from the previous step. My code looks as below. TEMP3=`sed -n '$TEMP2p' $FILEPATH/Log.txt` $TEMP2, I am getting from the previous step which is a numerical value(eg:3). ... (2 Replies)
Discussion started by: satyasrin82
2 Replies

5. Shell Programming and Scripting

Problem while passing variable

Within a shell im starting a terdata BTEQ session #!/bin/ksh set -x TEMP_DATABASE=`head -6 /home/karthik/para.txt|tail -1` TEMP_TABLE=`head -7 /home/karthik/para.txt|tail -1` bteq<<EOF .LOGON TEST/karthik,********; .EXPORT FILE=test1.txt; .set heading '' .set titledashes off; ... (4 Replies)
Discussion started by: mkarthykeyan
4 Replies

6. Shell Programming and Scripting

Passing a variable to sed command

Hi guys, I wanted to pass a variable to the sed command which tells which line to be deleted. a=2; echo $a; sed '$ad' c.out it is throwing an error. sed: 0602-403 "$a"d is not a recognized function. I even tried "$a" and \$a.. but it is of no use. Can you please correct me... (6 Replies)
Discussion started by: mac4rfree
6 Replies

7. Shell Programming and Scripting

Passing Variable in sed

Dear All, I want to print a file. First I tried with this sed '2q;d' filename it worked. But when i put following it is not working x=2; sed '$xq;d' filename Would any one suggest how to pass the variable? (7 Replies)
Discussion started by: saifurshaon
7 Replies

8. Shell Programming and Scripting

Passing a variable to sed or cut

:confused: Is it possible to send a variable to a sed or cut command? I have a test script as below: counter=1 while read line do # Test the file printf "$line" > temp$counter pref=$(cut c1-2000 $temp$counter | sed 's///g' | sed 's|.*PutTime\(.*)Origin.*|\1|') printf" let... (5 Replies)
Discussion started by: gugs
5 Replies

9. Shell Programming and Scripting

Passing a variable to sudo sed command

hi, dataParse(){ line="$@" name="cat /etc/passwd | grep "$line": | cut -f6 -d':'" eval $name > sam.txt 2>&1 sudo -u $line sed -n 's/data-1/&/p' $name/test.xml >> sam1.txt } Here i getting the homedir of the accounts and is set in name variable.which returns "/home/raju" which i... (3 Replies)
Discussion started by: sachin.tendulka
3 Replies

10. Shell Programming and Scripting

variable passing to sed

I m trying to pass variable to sed. export var=140920060731 sed -e '/$var/d' file but no luch so far..? any body has any idea abt it Is there any way to pass variable to SED? Thanks , Manish (2 Replies)
Discussion started by: Manish Jha
2 Replies
Login or Register to Ask a Question