[NTLUG:Discuss] A quick script anyone?
Leroy Tennison
leroy_tennison at prodigy.net
Fri Aug 1 00:17:16 CDT 2008
Daniel Hauck wrote:
> I know there are several very clever script writers here. You guys even
> seem to "think" in terms of scripting. I'm learning little by little
> all the great techniques you guys have shared in the past, but here's
> the present challenge:
>
> Parsing a CSV file and generating some output.
>
> I have a list where the first field is a number and the next two are
> text fields. A line would look like this:
>
> 10, "US", "United States of America"
>
> I just need to be able to parse this line to fill some variables. The
> output part I have down pretty well and will be able to transform this
> file (once I get the line parsing part down) into the output desired.
>
> It might be helpful to know that I will need to discard the quote
> characters and just need the text within.
>
>
> _______________________________________________
> http://www.ntlug.org/mailman/listinfo/discuss
>
Everyone has a way, I like AWK, it's less of a learning curve than Perl
and can handle most simple to medium-complex text processing tasks.
Reading the man page gives you virtually the whole language.
Using the following file names:
Input file: your.csv
Output file: final.txt
The AWK script: transform.awk
Create transform.awk containing the following:
BEGIN {FS=",";}
{var1 = $1;
var2 = substr($2,2,length($2)-2);
var3 = substr($3,2,length($3)-2);
# Put the scripting to do what you want to do with the variables here
}
Run the program:
gawk -ftransform.awk your.csv > final.txt
The above doesn't have typos (to the best of my knowledge), GNU AWK
named their AWK variant gawk. There's no space between the -f and
script name. Output is normally written to screen (making it easy to
debug a script, just leave off the output redirection) but in this case
redirected to a file.
More information about the Discuss
mailing list