Text files that consist of lines whose items are separated by commas are called CSV (comma-separated-value) data. They are often used for exchange of data between different software.
[Aside] I often enter numerical tables on a spreadsheet. It permits random browsing and occasional correction. It is also convenient for checking sums and averages which are often printed in the original material. About 1986, I wondered I would need to write a program for data entry. It was difficult to write such a program that permits retrospective correction of data. A student suggested "Microsoft Multiplan" which worked on MS-DOS version 2, and it was fine. On MS-DOS versions 3 and 5, "Lotus 1-2-3" flourished. I used "Ashisuto Calc" (Japanese version of "20-20"). Its operation was similar to Lotus 1-2-3, and it was cheaper. Now "Microsoft Excel" is dominant both on Windows and Mac. As a Linux user, I use "OpenOffice Calc". Its operation is similar to Microsoft Excel. In fact, I do not like its style of operation so much (I feel spreadsheet software of the age of DOS better). Therefore, as soon as I enter the data, I write them in CSV and handle them with Awk.
In Awk, there is a concept called "field separator". The value may be a comma, as set like this:
BEGIN { FS = "," }
Then items of a line of CSV data can be read as $1, $2, $3 etc.
This simple logic, however, presupposes that the items themselves do not contain commas. It may not always be true.
Usually, such programs that output data in CSV form write character strings (at least those including commas) in a quoted form. They also interpret commas within quotes as part of strings rather than field separators when they read them. But (as far as I know) Awk does not have such capability. As it reads a line, FS is handled first. There is no chance to interpret quotation marks before FS. So the above simple method does not work with such data that includes commas other than field separators.
Anyway, Awk reads the whole line as $0. Without resorting to the built-in field separator mechanism, we can write a program to interpret the line, double quotation marks first, commas second. Here is an example [readcsv1.awk]. In this example, items are stored in an array called "field[]". Then the items are printed one by one. For actual applications, the section of "test output" should be rewritten appropriately. It works even if the data contain commas, but it might not work with data that contain quotation marks or some other special characters in data items.
[Note added in 2009] A cell of a worksheet file of spreadsheet software may contain a "linefeed" (also known as "newline") control character as a part of its content text. When the worksheet is written out in the CSV format, the linefeed character may become a line break, so that one line in the worksheet may become two (or more) lines in the CSV text file. My Awk program just follows the line break in the text file, so it will not handle the cell position in the worksheet correctly in this situation. It is difficult to solve this problem by afterthought. I recommend that changing the linefeed characters to something else in the spreadsheet software before writing out in CSV.
[To the index page of "an awkward awker"] [To the entrance page of K. Masuda in English]