Created
November 3, 2012 08:58
-
-
Save skyscribe/4006646 to your computer and use it in GitHub Desktop.
To generate csv from text inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/awk -f | |
BEGIN{ | |
RS="\.\s* \[[0-9]+\]"; | |
FS="\n"; | |
OFS=","; | |
} | |
function find_next_field_until_regex(regex, i, result){ | |
result = ""; | |
for (; i < NF; i++){ | |
field = $i | |
sub(/,/, "\,", field); | |
sub(/^[ \t]*/, "", field); | |
if (field ~ regex){ | |
break; | |
} | |
result = result field; | |
} | |
printf("%s%s", result, OFS); | |
return i; | |
} | |
{ | |
if(NF>1){ | |
sub(/,/, "\,", $1); | |
printf("%s%s", $1, OFS); | |
i = 2; | |
i = find_next_field_until_regex("^[0-9]+", i); #discription | |
i = find_next_field_until_regex("^ph: ", i); #address | |
i = find_next_field_until_regex("www\\.", i); #phone | |
for (; i < NF; ++i){ | |
printf("%s", $i); | |
} | |
} | |
printf("\n"); | |
} | |
skyscribe@skyscribe-ubuntu:~/study/awk$ | |
skyscribe@skyscribe-ubuntu:~/study/awk$ vim filter.awk | |
skyscribe@skyscribe-ubuntu:~/study/awk$ cat filter.awk | |
#!/usr/bin/awk -f | |
BEGIN{ | |
RS="\.\s* \[[0-9]+\]"; | |
FS="\n"; | |
OFS=","; | |
} | |
function find_next_field_until_regex(regex, i, result){ | |
result = ""; | |
for (; i < NF; i++){ | |
field = $i | |
sub(/,/, "\,", field); | |
sub(/^[ \t]*/, "", field); | |
if (field ~ regex){ | |
break; | |
} | |
result = result field; | |
} | |
printf("%s%s", result, OFS); | |
return i; | |
} | |
{ | |
if(NF>1){ | |
sub(/,/, "\,", $1); | |
printf("%s%s", $1, OFS); | |
i = 2; | |
i = find_next_field_until_regex("^[0-9]+", i); #discription | |
i = find_next_field_until_regex("^ph: ", i); #address | |
i = find_next_field_until_regex("www\\.", i); #phone | |
for (; i < NF; ++i){ | |
printf("%s", $i); | |
} | |
} | |
printf("\n"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
. [135]Edwards Engineering Pty Ltd | |
Quality Structural Steel. Specialising In Fabrication And Steel | |
Stairs | |
21- 23 Ada Ave, Brookvale NSW 2100 | |
ph: (02) 9938 5320 | |
. [269]Diavolo Steel Fabrication | |
5 Humeside Drv, Campbellfield VIC 3061 | |
ph: (03) 9357 7947 | |
. [40]WH Williams Pty Ltd | |
Your Partner For High Quality Custom-Made Metal Products | |
Short lead times & unbeatable quality. Make us the first choice for | |
your entire sheetmetal laser cutting,bending,welding & more. | |
61- 77 Egerton St, Silverwater NSW 2128 | |
ph: (02) 9647 1277 | |
[41]www.whwilliams.com.au |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment