Escaping apostrophe using shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Escaping apostrophe using shell script
# 1  
Old 03-30-2006
Escaping apostrophe using shell script

Hi,

I am using the KSH shell. I am facing a problem of escaping apostrophe('), that is occuring in a variable.
I used the following command, but in vain

item=`echo $item|sed 's/'/\'/g'`

this code replaces the occurance of ' in an xml file to apostrophe(') symbol.
The output of the above piece of code is
sed: command garbled: s/'/\/g

Please Help,

Thanks in advance

MK
# 2  
Old 03-30-2006
Try putting the single quote into a variable (then you need to replace the single quotes around the sed commands with double to get the variable expanded)

Code:
Code
qot="'"
item=`echo $item|sed "s/'/$qot/g"`
print $item

cheers
# 3  
Old 03-30-2006
Thanks a ton,

MK
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find ' (apostrophe) by grep

hi linux expert how can i find apostrophe (') symbol in text file by grep? for example for find 'test, the command grep " \'test" * is not useful. Many Thanks samad (1 Reply)
Discussion started by: abdossamad2003
1 Replies

2. Shell Programming and Scripting

Escaping problem in a shell script

Hi, i made a gnuplot script which accepts a filename as parameter (using gnuplot -e) now i want to run this script from a shell script, the correct command (with a concrete parameter) looks like this: gnuplot -e 'name="filename.dat;col=2"' gplscript.gpl my shell script looks like this: ... (4 Replies)
Discussion started by: franko007
4 Replies

3. Shell Programming and Scripting

escaping metacharacters in paths for a shell command

I have a file which contains a list of paths separated by a new line character. e.g /some/path/to/a/file.png /some/path to/another/file.jpeg /some path/to yet/another/file Notice that these paths may contain metacharacters, the spaces for example are also not escaped. If I wanted... (5 Replies)
Discussion started by: cue
5 Replies

4. Shell Programming and Scripting

Remove apostrophe and a letter followed by it

Hi All, For example, I have some words like these: cabinet's school's field's you'll I know how to remove apostrophe from these words using this command: cat filename | tr -s "\'" ' ' But I am not sure how I can remove the letter followed by the apostrophe, so that my output... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

5. Shell Programming and Scripting

Replace apostrophe with backslash apostrophe

I'm coding using BASH and have a requirement to replace apostrophes with backslash apostrophes. For example below: I am here 'in my room' ok Would be changed to: I am here /'in my room/' ok The original text is coming from a field in a MySql database and is being used by another process that... (5 Replies)
Discussion started by: dbjock
5 Replies

6. Shell Programming and Scripting

script to replace a character with '(Apostrophe)

Hi Friends, I need a sed or awk based script to replace a character(alphabet) with ' (Apostrophe). I tried to use the following two commands 1) sed -e 's/a/'/' filename 2) awk '{sub("a","'",$1);print}' filename but both got failed. Any help on this. Thanks in advance.. (3 Replies)
Discussion started by: ks_reddy
3 Replies

7. Shell Programming and Scripting

escaping double-quotes inside the script?

I'm having a strange problem with escaping double-quotes. I have a script that looks like this: #!/bin/bash for HOST in `cat $INFILE | grep -v ^#` do for VFILER in `some_command` do echo " " echo -e '\E The problem with ssh command... (3 Replies)
Discussion started by: GKnight
3 Replies

8. Shell Programming and Scripting

bash script help: escaping the '*'

hi there. i have a simple bash script that reads a word from a text file one at a time, and if a '*' character is encountered, it prints a message. however it doesn't work as i expected. :o what am i doing wrong here? thanks very much for the help :) for word in `cat $DOC` do if... (18 Replies)
Discussion started by: mark_nsx
18 Replies

9. Shell Programming and Scripting

escaping * in korn shell

When trying to escape special character * - it doesn't seem to work. In korn shell trying to store a local variable as follows sample=test* echo $sample - gets all the file names starting with test* , instead i want to literally store the value test* into a variable. I tried escaping with \, with... (3 Replies)
Discussion started by: prekida
3 Replies

10. Solaris

escaping * in korn Shell under Sun Solaris

When trying to escape special character * - it doesn't seem to work. In korn shell trying to store a local variable as follows sample=test* echo $sample - gets all the file names starting with test* , instead i want to literally store the value test* into a variable. I tried escaping with \, with... (1 Reply)
Discussion started by: prekida
1 Replies
Login or Register to Ask a Question