Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

awk printf question
[cross-post comp.lang.awk, comp.unix.shell]

I have a file whose record delimiters seem to be "Directions" and
"More" (i.e., I need the data between each, however they need to be
joined and comma-delimited.  Kind of the inverse of csv parser.

For example, given:

More Info | Map | Directions

Milford Pharmacy & Wellness Center
931 State Rte 28 Ste 205
Milford, OH 45150-1990
(513) 831-8211

More Info | Map | Directions

Alexandria Discount Drugs
7857 Alexandria Pike
Alxndra, KY 41001-1045
(859) 635-2171

More Info | Map | Directions

should return

"Milford Pharmacy & Wellness Center","931 State Rte 28 Ste
205","Milford","OH",45150-1990","(513) 831-8211"
"Alexandria Discount Drugs","7857 Alexandria
Pike","Alxndra","KY","41001-1045","(859) 635-2171"

Note--There are quotes around the city, state, and zip to create
discreet fields.

Report this thread to moderator Post Follow-up to this message
Old Post
sandy_eggo
03-29-08 03:00 AM


Re: awk printf question
["Followup-To:" header set to comp.unix.shell.]
On 2008-03-29, sandy_eggo <cpmadrid@gmail.com> wrote:
>
>
> [cross-post comp.lang.awk, comp.unix.shell]
>
> I have a file whose record delimiters seem to be "Directions" and
> "More" (i.e., I need the data between each, however they need to be
> joined and comma-delimited.  Kind of the inverse of csv parser.
>
> For example, given:
>
> More Info | Map | Directions
>
> Milford Pharmacy & Wellness Center
> 931 State Rte 28 Ste 205
> Milford, OH 45150-1990
> (513) 831-8211
>
> More Info | Map | Directions
>
> Alexandria Discount Drugs
> 7857 Alexandria Pike
> Alxndra, KY 41001-1045
> (859) 635-2171
>
> More Info | Map | Directions
>
> should return
>
> "Milford Pharmacy & Wellness Center","931 State Rte 28 Ste
> 205","Milford","OH",45150-1990","(513) 831-8211"
> "Alexandria Discount Drugs","7857 Alexandria
> Pike","Alxndra","KY","41001-1045","(859) 635-2171"
>
> Note--There are quotes around the city, state, and zip to create
> discreet fields.

You could use a blank line as the record separator and discard records
beginning with "More Info".
awk -F\\n 'BEGIN{RS="";OFS="\",\""}
/^More Info/{next}
{$0="\"" $0 "\""; sub(/,/,"",$3);gsub(/ /,OFS,$3)}
{print}'

Report this thread to moderator Post Follow-up to this message
Old Post
Bill Marcum
03-29-08 03:00 AM


Re: awk printf question

On 3/28/2008 7:04 PM, sandy_eggo wrote:
> [cross-post comp.lang.awk, comp.unix.shell]
>
> I have a file whose record delimiters seem to be "Directions" and
> "More" (i.e., I need the data between each, however they need to be
> joined and comma-delimited.  Kind of the inverse of csv parser.
>
> For example, given:
>
> More Info | Map | Directions
>
> Milford Pharmacy & Wellness Center
> 931 State Rte 28 Ste 205
> Milford, OH 45150-1990
> (513) 831-8211
>
> More Info | Map | Directions
>
> Alexandria Discount Drugs
> 7857 Alexandria Pike
> Alxndra, KY 41001-1045
> (859) 635-2171
>
> More Info | Map | Directions
>
> should return
>
> "Milford Pharmacy & Wellness Center","931 State Rte 28 Ste
> 205","Milford","OH",45150-1990","(513) 831-8211"
> "Alexandria Discount Drugs","7857 Alexandria
> Pike","Alxndra","KY","41001-1045","(859) 635-2171"
>
> Note--There are quotes around the city, state, and zip to create
> discreet fields.

awk -v RS= -F'\n' -v OFS='","' '!/^More/{gsub(/[, ]+/,OFS,$3);print "\""$0"\
""}'
file

Ed.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-29-08 09:00 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 03:42 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.