Replacing values in strings using shell

Hi,
I am trying to figure out something simple but it's taking me pretty much a whole day and I still can't make it work.  I need an expert help!!

I have the following record and want to replace ""string_value" to "string_value
for example, I want to replace ""134AKSaa837" <zip:4793284928@xxx.xxx.xx.xxx>;tag=abcdefg"
to "134AKSaa837 <zip:4793284928@xxx.xxx.xx.xxx>;tag=abcdefg"

A sample record:

"ZIP",,""134AKSaa837" <zip:4793284928@xxx.xxx.xx.xxx>;tag=abcdefg","<zip:17186230106@xx.xx.xxx.x>",1,4,"16:00:02.066 UTC MAR 13 2011","16:00:28.342 UTC MAR 13 2011","16:00:32.085 UTC MAR 13 2011",1,"ABCD-KLDP-INT","RCCECF
S","localhost:31162488","G111","RKCRCFA","0.0.0.0",0,"zz.zz.zzz.z",12578,"ZZZZ-ZZZZ-INT",,"SMITH, DAVID <zip:4793284928@xxx.xxx.xx.xxx>;tag=abcdefg",ANCGD

If you have a great idea, please let me know.  I am using ksh, sed, and/or awk.

Thanks,
Sammy
alpswonAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor Commented:
The one in http:#35337318 should do it then, but the only thing to be wary of is that if you have something along these lines:

aaa,""bbbbbb ccccc, eee,1,fffff iiii",2222
aaa,""bbbbbbccccc, "eee",1,""fffff" iiii",2222

they would be transformed into:

aaa,"bbbbbb ccccc, eee,1,fffff iiii,2222
aaa,"bbbbbbccccc, eee",1,""fffff" iiii",2222

which is probably not what you want.

Is there a guarantee that if we find a double quote that the matching single quote will be in the appropriate place? If it's missing, then we get the above scenarios.
0
 
point_pleasantCommented:
I have the following record and want to replace ""string_value" to "string_value

try this


cat record | tr -d "\"" | awk '{sub(/^/,"\"");print}'
0
 
balasundaram_sCommented:
Simple, using sed, substitute "" with ", like below

sed s/""/"/g line > target_line

Its just a idea for the whole script.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
point_pleasantCommented:
or if you want the first and last character of the record to be a quote character and all quotes inbetween stripped then use

cat record | tr -d "\"" | awk '{sub(/^/,"\"");print}' | awk '{sub(/$/,"\"");print}'
0
 
alpswonAuthor Commented:
point pleasant,

your suggested solution is not what I am looking for.  Your solution replace all double quotes, except the first and last one.  
What I am looking for is I want to replace two double quotes to a single double quote and then remove a following double quote.  

Original string:
"ZIP",,""134AKSaa837" <zip:4793284928@xxx.xxx.xx.xxx>;tag=abcdefg","ancd", 123"

Open in new window


After edit:
"ZIP",,"134AKSaa837 <zip:4793284928@xxx.xxx.xx.xxx>;tag=abcdefg","ancd", 123"

Open in new window


double quotes around 134AKSaa837 needs to be removed after the edit.

Thanks,
Sammy
0
 
point_pleasantCommented:
ok even easier

 cat junksed | awk '{sub(/\"\"/,"\"");print}'


will replace all double quotes with a single quote
0
 
alpswonAuthor Commented:
Point Pleasant
Please take a closer look at the original string and updated string.  It's not just a simple double quote to single quote replacement.  The replacement should only occur when finding two double quotes and a following double quote.

BEFORE:
""134AKSaa837" zip:4793284928;tag=abcdefg"

Open in new window


AFTER:
"134AKSaa837 zip:4793284928;tag=abcdefg"

Open in new window

0
 
käµfm³d 👽Commented:
Does this do it?
sed -e 's/"\("[^"]*\)"/\1/' inputfile

Open in new window

0
 
käµfm³d 👽Commented:
Actually, I need to correct that:
sed -e 's/"\("[^ ]*\)"/\1/' inputfile

Open in new window

0
 
alpswonAuthor Commented:
kaufmed

You are almost there. Your expression works beautifully, except one case.  Please see the CASE 2.

INPUT:
CASE 1: aaa,""bbbbbb" ccccc", "eee",1,""fffff" iiii",2222
CASE 2aaa,""bbbbbb"ccccc", "eee",1,""fffff" iiii",2222

OUTPUT:
CASE 1 RESULT: aaa,"bbbbbb ccccc", "eee",1,"fffff iiii",2222 (good)
CASE 2 RESULT: aaa,"bbbbbb"ccccc, "eee",1,"fffff iiii",2222  (still has a double quote)

0
 
alpswonAuthor Commented:
kaufmed,

sed -e 's/"\("[^"]*\)"/\1/' inputfile solution works for my problem.  I will not have the case aaa,""bbbbbb ccccc, eee,1,fffff iiii",2222 and aaa,""bbbbbbccccc, "eee",1,""fffff" iiii",2222 should be transformed into aaa,"bbbbbbccccc, eee",1,""fffff" iiii",2222.  

Thank you so much for your help!
Sammy
0
 
käµfm³d 👽Commented:
NP. Glad to help  : )
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.