[NTLUG:Discuss] A quick script anyone?
Eric Schnoebelen
eric at cirr.com
Thu Jul 31 10:57:03 CDT 2008
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.
--
Eric Schnoebelen eric at cirr.com http://www.cirr.com
"With sufficient thrust, pigs fly just fine..."
-- RFC 1925 "The Twelve Networking Truths"
More information about the Discuss
mailing list