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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
käµfm³d 👽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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.