Page 1 of 1

How to extract a named CSV field

Posted: Thu Nov 06, 2014 4:53 pm
by DataMystic Support
Below is the text of a TextPipe Scripting filter, which can be used to extract a named column from a text file, even when that column changes position from file to file.

Code: Select all

'the field name to extract
dim field_name
field_name = "Zip Code"
dim a 
dim first_line
dim c

function processLine(line, EOL)

	a = Split( line, "," )
	if first_line then
    	c = 0
		for each x in a
	        'identify which field number it is
    		c = c + 1
            TextPipe.logError x
            if x = """" & field_name & """" then exit for
            TextPipe.logError c
        first_line = false
        c = c - 1
    end if

	processLine = a(c) & EOL     
end function

sub startJob()
end sub

sub endJob()
end sub

function startFile()
  startFile = ""
  first_line = true
end function

function endFile()
  endFile = ""
end function