[NTLUG:Discuss] A quick script anyone?
Daniel Hauck
daniel at yacg.com
Thu Jul 31 12:54:17 CDT 2008
Eric Schnoebelen さんは書きました:
> Daniel Hauck writes:
> - 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.
>
> Here's a start for you.. You're still going to have quotes
> around the strings, but that may be useful for later evaluation
> of the strings.
>
> #!/bin/sh
> #
> OIFS="${IFS}"
> IFS="${IFS},"
> while read num short long; do
> echo ${num}:${short}:${long}
> done
> IFS="${OIFS}"
>
> The "trick" is changing the Input Field Seperator string to
> include the comma. Doing that, the shell naturally splits on the
> comma, as well as other white space. By adding it to the
> standard IFS (which is [\t\ \n]), the white space and comma are
> gobbled and used as seperators between words.
>
That's just about perfect. I just need a way to get rid of the quotes.
More information about the Discuss
mailing list