find single quote in a string and replace it


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers find single quote in a string and replace it
# 1  
Old 11-03-2010
find single quote in a string and replace it

Hi,

I have variable inside shell script - from_item.
from_item = 40.1'1/16

i have to first find out whether FROM_ITEM contains single quote(').
If yes, then that need to be replace with two quotes ('').

How to do it inside shell script? Please note that inside shell script........
# 2  
Old 11-03-2010
Code:
#!/bin/ksh

FROM_ITEM="40.1'1/16"

echo "OLD:  ${FROM_ITEM}" 

#
# If you actually want two single quotes.
#
SINGLE_QUOTE=$(echo ${FROM_ITEM} | sed "s/'/''/")

#
# If you actually want one double quote.
#
DOUBLE_QUOTE=$(echo ${FROM_ITEM} | sed "s/'/\"/")

echo "SINGLE QUOTE:  ${SINGLE_QUOTE}"

echo "DOUBLE QUOTE:  ${DOUBLE_QUOTE}"


jsmith:..ksh $ ./tsed.ksh  
OLD:  40.1'1/16
SINGLE QUOTE:  40.1''1/16
DOUBLE QUOTE:  40.1"1/16


Last edited by jsmithstl; 11-03-2010 at 07:50 AM.. Reason: Changed code to include double quote example.
# 3  
Old 11-03-2010
thanks jsmithstl for reply.

I have used this already and works fine at unix prompt.

When used same inside shell script , gives following error. can u please help?

Error on line 218:
(SQR 3706) Missing Comma.
from_item=$(echo $from_item | sed "s/'/''/g")

Error on line 218:
(SQR 3710) Unknown argument type.
from_item=$(echo $from_item | sed "s/'/''/g")
# 4  
Old 11-03-2010
Can you post your entire script and what shell you are using?
# 5  
Old 11-04-2010
You can do something like this:
Code:
case $from_item in
  *\'*) echo "$from_item contains a single quote"
        echo "here it is gone: ${from_item%\'*}${from_item#*\'}" 
        echo "here ${from_item%\'*}\"${from_item#*\'} has a double quote"
esac

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to split string on single quote as delimiter

I have a variable that contains the following string: FPATH=-rw-rw-r-- 1 user1 dba 0 Aug 7 13:14 /app/F11.3/app/cust/exe/filename1.exe' -rw-rw-r-- 1 user1 dba 0 Aug 19 10:09 /app/app/F11.3/app/cust/sql/33211.sql' -rw-r--r-- 1 user1 dba 0 Aug 6 17:20 /app/F11.2/app/01/mrt/file1.mrt' I... (7 Replies)
Discussion started by: mohtashims
7 Replies

2. UNIX for Beginners Questions & Answers

Enclose String in single quote

I have a comma separated file which contains data like; File header: ID_WVR,SAK_WVR_SVC,DSC_WVR_WVC,SAK_PROCEDURE,CODES,CDE_PROC_MOD ,CDE_PROC_MOD_2 ,CDE_PROC_MOD_3 File Detail: AMR,5100,Total Services,305,D0120,,, AMR,5101,Periodic Services,40702,H2011,U1,, AMR,5112,Day... (4 Replies)
Discussion started by: jnrohit2k
4 Replies

3. Shell Programming and Scripting

Replacing all but the first and last double quote in a line with a single quote with awk

From: 1,2,3,4,5,This is a test 6,7,8,9,0,"This, is a test" 1,9,2,8,3,"This is a ""test""" 4,7,3,1,8,"""" To: 1,2,3,4,5,This is a test 6,7,8,9,0,"This; is a test" 1,9,2,8,3,"This is a ''test''" 4,7,3,1,8,"''"Is there an easy syntax I'm overlooking? There will always be an odd number... (5 Replies)
Discussion started by: Michael Stora
5 Replies

4. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies

5. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

6. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

7. Shell Programming and Scripting

replace a single quote using sed command.

Hi, How do I replace the charachters = '*' to = '^' using Sed commands. As I have several * in the script, thus I have to replace the single quotes too. Please let me know any solution. I have tried the escape character backslash '\' but it doesnt help. sed "s/= \'*\'/=... (2 Replies)
Discussion started by: prachifun
2 Replies

8. Shell Programming and Scripting

Replace a string in quote to uppercase

Hi, I need to find all strings in single quote and change the case of the string to UPPER. Example: Input: xyz abc ccc 'zxxx7U' dfsdf sdfdd aaa 'oR77' and 'or88' Output: xyz abc ccc 'ZXXX7U' dfsdf sdfdd aaa 'OR77' and 'OR88' Appreciate your help!! Thanks Selva (6 Replies)
Discussion started by: selkum
6 Replies

9. Shell Programming and Scripting

Regex in grep to match all lines ending with a double quote (") OR a single quote (')

Hi, I've been trying to write a regex to use in egrep (in a shell script) that'll fetch the names of all the files that match a particular pattern. I expect to match the following line in a file: Name = "abc" The regex I'm using to match the same is: egrep -l '(^) *= *" ** *"$' /PATH_TO_SEARCH... (6 Replies)
Discussion started by: NanJ
6 Replies

10. UNIX for Dummies Questions & Answers

replace part of single string in a file

hi! i have a file consisting of the following lines: (BTW, = space) . . . 12ME_T1mapping_flip30bshortf 13DCE_whole_brainbshortf 13DCE_3Dbshortf . . . the list of scans starts at 1 and goes on sometimes up to 60 scans. i would like to change only the lines that contain 'whole' to... (2 Replies)
Discussion started by: nixjennings
2 Replies
Login or Register to Ask a Question