Problem removing $ using sed in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem removing $ using sed in script
# 1  
Old 08-02-2012
Problem removing $ using sed in script

I am trying to use the sed command to remove any character that is passed as a parameter to the script. I may want to replace any character in the file (-, =, $, space). I'm running into an issue when passing the $.

This is a typical row in the file.
Code:
"000000B553","--INTERNAL USE ONLY-PCG--",="07041200033", $26.70,$18.23,$8.47,04/12/2007,="00015251",198181

Code (test_script.sh):
Code:
#!/bin/ksh

REMOVE_CHAR=${1}   
sed "s/""${REMOVE_CHAR}""//g" orig.csv > corrected.csv
exit 0

If I run the following, the $ is not removed.

test_script.sh "$"

Can someone please help?Smilie

Thank you.

Last edited by Scrutinizer; 08-02-2012 at 11:44 AM.. Reason: code tags for data too
# 2  
Old 08-02-2012
Try
Code:
sed "s/\\${REMOVE_CHAR}//g"

and, btw, why don't you use $1 in lieu of $REMOVE_CHAR?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-02-2012
That worked. Thank you so much!

Also, I could use $1 for this, but this is just a piece of a larger script where there are 6 different parameters that are named for clarity. I just didn't change that part.
# 4  
Old 08-02-2012
For removing characters
Code:
tr -d '$'  < orig.csv > corrected.csv

is more straightforward.
These 2 Users Gave Thanks to hipe For This Post:
# 5  
Old 08-02-2012
Quote:
Originally Posted by RudiC
Try
Code:
sed "s/\\${REMOVE_CHAR}//g"

and, btw, why don't you use $1 in lieu of $REMOVE_CHAR?
Probably readability, even without comments you know what the sed statement is doing due to the variable name.

Quote:
If I run the following, the $ is not removed.

test_script.sh "$"

Can someone please help?

Thank you.
Edit: Hipe has the way you probably should do this.

It's because $ is a special character in sed and you aren't escaping it. You either need to have an if statement that checks what the character is, or you need to escape it when it's being passed to the shell, IE

Code:
$ ./test.sh \\$   
$ cat corrected.csv
"000000B553","--INTERNAL USE ONLY-PCG--",="07041200033", 26.70,18.23,8.47,04/12/2007,="00015251",198181

You can't just use one escape character, as it'll eat it before it gets to your sed statement. The other route again is simply an if statement. I know ksh will handle regex matching, but I don't know the format offhand, but your regex would simply be something like [.*$^], which are some of the characters sed will gobble up.

Last edited by Vryali; 08-02-2012 at 11:54 AM..
# 6  
Old 08-02-2012
You're right, @hipe. But the OP used the terms remove and replace in his original request.
# 7  
Old 08-02-2012
The sed suggestion with the escape before the variable expansion would work for special characters that need to be escaped so that they lose their special meaning, however there are also characters that gain a special meaning when escaped and then it would fail. For example: (){}123456789( and it would also fail for the empty string)..

Last edited by Scrutinizer; 08-02-2012 at 12:08 PM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed or awk script problem

Hi All I am having a file as shown below File1# modyle 1 { test jsj hhsjh 13e3 jsjjs } memP(dbg_trace) { ajjs jsjs jsjs Test(claer) { jsjs (7 Replies)
Discussion started by: kshitij
7 Replies

2. Shell Programming and Scripting

sftp script - problem removing files remotely

Hello all, After searching through the similar topics and not finding a working solution, I decided to join and post my question (and maybe kill time and help other users). Essentially I am trying to get a file from the sftp and then delete it after the file is pulled. all the sftp commands... (3 Replies)
Discussion started by: MarcMaiden
3 Replies

3. Shell Programming and Scripting

SED removing CR/LF

I have a source text file that contains something like this: <start_template> bool function( void *<var_name> ) { // Blah Blah } </start_template> Following is used to remove this template from source file. template=$(sed -n -e '/<start_template>/,/<\/start_template>/{... (4 Replies)
Discussion started by: gee22
4 Replies

4. Shell Programming and Scripting

Shell Script problem with sed

Hello i have a shell script which will go in some folders using for loop, build some programs with gcc and zip the output. then i have to paste the file of that zip file into a yml file which will be further used by a ruby watir script. PROJECT_PATH=/uploads/$PROJECT_FOLDER zip -rq ... (0 Replies)
Discussion started by: vikki1986
0 Replies

5. Shell Programming and Scripting

Sed Script Problem

My file is having the contents like this AND1 (A) { non : hafe on: tc ma : nite } AND1 (B) { non : hafe1 on: tc1 ma : nite1 } AND1 (C) { non : hafe3 on: tc2 ma : nite3 } I have written a sed script to search the patterns AND1(A) and AND1 (B) and delete the entire... (2 Replies)
Discussion started by: shalini_008
2 Replies

6. Shell Programming and Scripting

Problem using sed in script

Hey guys, I'm looking to write a script that takes a variable line number and prints that line via sed, but I'm having an issue with the syntax. As I understand it, the sed syntax to print line number 52 (for example) is as follows: sed -n '52p' sed '52!d' sed '52q;d' But how can you... (2 Replies)
Discussion started by: wandergeek
2 Replies

7. Shell Programming and Scripting

Removing a character using sed

Hi, I have a file with the text below. How do i remove the character "%" from the text file using sed ? Can anybody help ? 0% 68% 72% 0% 54% 33% 75% 24% 6% 59% 77% 77% 33% (6 Replies)
Discussion started by: Raynon
6 Replies

8. Shell Programming and Scripting

Removing lines with sed

Here is some code output that I have: architecture ppc cputype CPU_TYPE_POWERPC cpusubtype CPU_SUBTYPE_POWERPC_ALL offset 4096 size 184464 align 2^12 (4096) architecture ppc64 cputype CPU_TYPE_POWERPC64 cpusubtype CPU_SUBTYPE_POWERPC_ALL offset 192512 ... (5 Replies)
Discussion started by: pcwiz
5 Replies

9. Shell Programming and Scripting

problem with sed command in shell script.

Guys, I've a problem in the "sed" command used in my shellscripts This is the problamatic line in my shell script: sed -e 's/${line1}/${line1_m}/g' prod_hier_1234.txt > test.txt It doesn't do the job of replacing the string stored in variable 'line1' to 'line1_m'. However If I replace the... (10 Replies)
Discussion started by: bhagat.singh-j
10 Replies

10. UNIX for Dummies Questions & Answers

sed modify problem in script

I am having problems with the following "sed" command only when it is issued within a bash script. #!/bin/bash cat config.xml | sed -e 's/yes/no/g' > newconfig.xml When I enter this command from the command line it works like a charm, but when run in a script as shown it "zero's out" my... (2 Replies)
Discussion started by: darthur
2 Replies
Login or Register to Ask a Question