How to load .txt file in data source and transform it please any help is appreciated

ABHAY_M
Tera Guru

find_real_file.png

 this is the sample data of text file  first coloumn represents location field and second column asset tag how to load this data in data source if parsing then how do i parse with script 

Thank you 

1 ACCEPTED SOLUTION

Jon23
Mega Sage

One approach is to use the 'Custom (Parse by Script)' format for a data source.

Based on the file you provided, the following parsing script can be used in the data source:

Parsing Script

// The input value can be accessed through the variables named "line", "lineNumber" and "result"
// The function uses result variable to return parse result back. 
(function(line, lineNumber, result) {

    var map = {};
    var items = line.split("|");
    map['location'] = items[0];
    map['asset'] = items[1];
    result.addRow(map);

})(line, lineNumber, result);

Data Source Record

find_real_file.png

 

 

View solution in original post

13 REPLIES 13

Xhare
Tera Contributor

Hi @Jon23 ,

 

Do you have idea, on what to use if I will retrieve the data start from row 15. My column header starts in row 14.

Xhare_0-1726217932880.png

 

 

Hi @Xhare 

You should be able to wrap the code with an 'if' and not start processing until the lineNumber is reached.  For example:

// The input value can be accessed through the variables named "line", "lineNumber" and "result"
// The function uses result variable to return parse result back. 
(function(line, lineNumber, result) {

    if (lineNumber > 13) { //  <<<lineNumber starts at 0
        var map = {};
        var items = line.split(",");
        map['UserName'] = items[0];
        map['LastName'] = items[1];
        map['FirstName'] = items[2];
        map['MiddleName'] = items[3];
        map['Organization'] = items[4];
        result.addRow(map);
    }

})(line, lineNumber, result);

Xhare
Tera Contributor

Hi @Jon23 ,

 

I'm using below script, but still getting data from row 1 until the end of row.

(function(line, lineNumber, result) {
    if (lineNumber > 14) {
        var headers = gs.getProperty("ccc.ftp.log.extract.headers").split("|");
        var items = line.replace(/[^\x20-\x7E]/g, '').split(",");
        var map = {};

        for (var i = 0; i < items.length; i++) {
            map[headers[i]] = items[i];
        }

        result.addRow(map);
    }
})(line, lineNumber, result);

@Xhare 

Can you check your end of line (EOL) characters on the file?

Depending on the system creating the file it could be a line feed which may be causing the issue.  If it is, try converting EOL to carriage return/line feed.