?
Solved

awk, sed or regex to process csv file

Posted on 1998-01-02
4
Medium Priority
?
448 Views
Last Modified: 2013-12-26
perl is not available to me and i have an excel-generated csv file that i need to process as follows:
  1.  remove all commas within double-quotes
      (but retain comma delimiters)
  2.  remove all double-quotes
  3.  replace all spaces in each field with @!%

sample record:
1,22222222,333,"company, Inc.",,444,USPS,company name,,123 elm st,Ste 800,Miami,FL,33131,


can anyone help with this?
0
Comment
Question by:luke_airig
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:luke_airig
ID: 1295888
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
ID: 1295889
If your sed is POSIX conforming,
 s/\(.*\)/"\1"/; s/\("\([^"]*\)"\)*,*/\2/g; s/ /@!%/g
should produce
1,22222222,333,company@!%Inc.,,444,USPS,company@!%name,,123@!%elm@!%st,Ste@!%800,Miami,FL,33131,
from your sample record.
0
 

Accepted Solution

by:
ashishkh earned 100 total points
ID: 1295890
Try this script:
sed -e "s/\("\([^",]*\),\([^",]*\)"\)/\2\3/g; s/ /@!%/g" infile

0
 
LVL 84

Expert Comment

by:ozo
ID: 1295891
ashishkh, that works with a single quoted comma,
(assuming your shell can handle the -e quoting,
sed -e 's/\("\([^",]*\),\([^",]*\)"\)/\2\3/g; s/ /@!%/g'
or
sed -e 's/\("\([^",]*\),\([^",]*\)"\)/\2\3/g; s/ /@\!%/g'
may work better if not)
but not with multiple commas, as in
"a,b,c","d,e,f"

0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question