The C1Awk Control > Parsing Text Files |
The following code counts the words in a text file using the C1Awk control, without any string or file statements:
Example Title |
Copy Code
|
---|---|
awk.FileName = Filename awk.Action = acScan ' Gets called right after the file is opened Sub awk_Begin () NWords = 0 End Sub ' Gets called after each line is read Sub awk_Scan () NWords = NWords + awk.NF ' number of fields = number of words End Sub ' Gets called right after the file is closed Sub awk_End () MsgBox Filename & " has " & Format$(NWords) & " words." End Sub |
You can easily access individual fields using the F property. Suppose, for example, that you wanted to add all the values on the second field of a file imported from a spreadsheet program.
The code below shows how this is done.
Example Title |
Copy Code
|
---|---|
awk.FileName = "spread.txt" awk.Action = acScan ' Gets called after the file is opened Sub awk_Begin () Total = 0 End Sub ' Gets called after each line is read Sub awk_Scan () Total = Total + Val(awk.F(2)) ' second field End Sub ' Gets called after the file is closed Sub awk_End () MsgBox "The total is " & Format$(Total) & "." End Sub |