Solved

awk, sed or regex to process csv file

Posted on 1998-01-02
4
418 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
  • 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 50 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now