SED script to backslash special characters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED script to backslash special characters
# 1  
Old 05-24-2010
SED script to backslash special characters

I have a shell script that I have written to be a kind of to-do/notepad that's quickly executable from the command line. However, special characters tend to break it pretty well.

Ie: "notes -a This is an entry." works fine.
"notes -a This is (my) entry." will toss back a bash syntax error on the (.

I was curious if there is an easier way of correcting special characters than writing a sed script to backslash them automatically. I realize that I could do it manually, but it's supposed to be a quick-and-easy program.

Current plan is to write a sed script to backslash certain characters, but I'm open to suggestions.
# 2  
Old 05-24-2010
I've usually gone with awk
Code:
echo 'This$ has *special char()'  | awk '{gsub( /[(*|`$)]/, "\\\\&"); print $0}'

# 3  
Old 05-24-2010
Not quite sure if this is what you're looking for, but recent bash versions support the non standard format specification q with printf:

Code:
bash-4.1.5(1)[t]$ cat infile 
notes -a This is (my) entry.
This$ has *special char()

Code:
bash-4.1.5(1)[t]$ while IFS= read -r;do printf '%q\n' "$REPLY";done<infile
notes\ -a\ This\ is\ \(my\)\ entry.
This\$\ has\ \*special\ char\(\)

Otherwise you can use Perl:

Code:
bash-4.1.5(1)[t]$ perl  '-ple$_=quotemeta' infile 
notes\ \-a\ This\ is\ \(my\)\ entry\.
This\$\ has\ \*special\ char\(\)


Last edited by radoulov; 05-24-2010 at 06:25 PM..
# 4  
Old 05-25-2010
using quotemeta in perl is brilliant choice.
# 5  
Old 05-25-2010
You need to quote the parameter on the command line to stop shell interpreting the special characters. The error message from the () is before your script runs.
The double quotes in this example are important.

Code:
notes -a "This is (my) entry."

# 6  
Old 05-26-2010
Thank you for all of the responses. Amusingly enough, quoting the input on the command line seems to solve the problems I was having. I don't know if I can alias something to auto-quote the entries, or if I will have to do it manually.

I did walk through all of these, radoulov's perl script was really neat, I'm going to keep that quotemeta function in mind for other projects. I couldn't get jim mcnamara's awk working, however.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Tcsh script - sed command for special characters

Hi, I am using sed command to replace following line in gz file- sed -n 's/""COS_12_TM_1" ( 1101110101001001010011110100000010110100010010000000100000000010XX010000000 )"/""COS_12_TM_1" ( 110111010100100101001111MM00000010110100010010000000100000000010XX010000000 )"/g' filename.gz $x=... (4 Replies)
Discussion started by: Preeti Chandra
4 Replies

2. Shell Programming and Scripting

Replace special characters with backslash and character

Hi, I have a string wherein i need to replace special characters with backslash and that character. Ex: If my string is a=qwerty123@!, then the new string should be a_new=qwerty123\@\!\, Thanks (3 Replies)
Discussion started by: temp_user
3 Replies

3. Shell Programming and Scripting

sed in a while loop with special characters

I have the foolowing data file: File1 <p name="A">5004</p> <p name="B">5004</p> <p name="C">5004</p> <p name="A">15004</p> <p name="B">15004</p> <p name="C">15004</p> In a while loop using sed (100 of line need to be replace), I need the output to File3:... (2 Replies)
Discussion started by: bobo
2 Replies

4. Shell Programming and Scripting

Escape special characters in SED

Need help in escaping special characters in sed command. Here is the the string which i am trying to find a replace with From :- REQUEST_TYPE=PIXEL&amp;MSG_ID={//MESSAGE_ID} To :- REQUEST_TYPE=PIXEL&amp;MSG_ID= X_EDELIVERY_MESSAGE_ID &amp; BATCH_ID= X_EDELIVERY_BATCH_ID Here is the sed command i am... (2 Replies)
Discussion started by: aakishore
2 Replies

5. Shell Programming and Scripting

SED with Special characters

Hello All Seeking the right one SED command. My attempt is: From orginal.txt by SED to target.txt sed -i "/('outbound-callerid/a\$ext->add($context, $exten, '', new ext_SipAddHeader('P-Preferred-Identity', '<sip:${CALLERID(nummer)}@carrier.com>'));" orginal.txtWhat am make wrong?:wall: ... (5 Replies)
Discussion started by: mdbinder
5 Replies

6. Shell Programming and Scripting

Using sed to replace special characters

Hi everyone I have file1 contains: '7832' ' 8765 6543 I want a sed command that will format as: '7832' , '8765' , '6543' I tried sed -e s/\'//g -e 's/^*//;s/*$//' file1 > file2 sed -e :a -e '$!N; s/\n/ /; ta' file2 which gives: 7832 8765 6543 I need some help to continue with... (5 Replies)
Discussion started by: nimo
5 Replies

7. Shell Programming and Scripting

sed with many special characters

I started with this: counter1=1 cp file.txt file_${counter1}.tmp while read name1 do echo $name1 counter2=`expr $counter1 + 1` sed /'${name1}'/d file_${counter1}.txt > file_${counter2}.txt counter1=`expr $counter1 + 1` done < source.txtsource.txt contains the... (1 Reply)
Discussion started by: lakanino
1 Replies

8. Shell Programming and Scripting

sed with special characters

Hi, I am reading a file (GC_JAR.log) which has entries like: 511725.629, 0.1122672 secs] 525268.975, 0.1240036 secs] 527181.835, 0.2068215 secs] 527914.287, 0.2884801 secs] 528457.134, 0.2548725 secs] I want to replace all the entries of "secs]" with just "secs" Thus, the output... (4 Replies)
Discussion started by: itzz.me
4 Replies

9. Shell Programming and Scripting

help on sed replacing special characters

Hello, I have a file with many lines with below format: \abc\\1234 jkl\\567 def\\345 \pqr\\567 \xyz\\234 Here, i need to do 2 things. 1. replace \\ with \ 2. remove starting \ so output to be as below: (11 Replies)
Discussion started by: prvnrk
11 Replies

10. Shell Programming and Scripting

Sed and awk backslash characters

Hi, I have a variable read from user input: PROFILESROOTDIR="\\194.185.82.188\CMSRepository\EncodingProfiles" awk -F"=" -v gr=$PROFILESROOTDIR '/ProfilesRootDirectoryFromXOEMachine/{$2=gr;}1' OFS="=" $CFGFILE > "${CFGFILE}_new" For this awk to work properly I need to replace in the... (7 Replies)
Discussion started by: potro
7 Replies
Login or Register to Ask a Question