For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2008 > awk printf question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author awk printf question
sandy_eggo

2008-03-28, 10:00 pm

[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.
Bill Marcum

2008-03-28, 10:00 pm

["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}'
Ed Morton

2008-03-29, 4:00 am



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.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com