user1980542
user1980542

Reputation:

Javascript, strip/ignore last line of CSV file

I have an CSV file looking like this:

Dato,11/2012,12/2012,01/2013,02/2013,03/2013,04/2013,05/2013,06/2013,07/2013
Freespace,4942,4729,4930,4889,4777,4799,4302,4567,4600
Allokeret,6784,7272,7079,7112,7232,7210,7707,7441,7408

What you cannot see in the above, is that there is a blank (empty) line below these 3.

The above file is created on the mainframe, and i have verified that there is no control characters there, which could cause the extra blank line to appear, demonstrated through the hex characters below:

Dato,11/2012,12/2012,01/2013,02/2013,03/2013,04/2013,05/2013,06/2013,07/2013    
C8A96FF6FFFF6FF6FFFF6FF6FFFF6FF6FFFF6FF6FFFF6FF6FFFF6FF6FFFF6FF6FFFF6FF6FFFF    
4136B1112012B1212012B0112013B0212013B0312013B0412013B0512013B0612013B0712013    
Freespace,4942,4729,4930,4889,4777,4799,4302,4567,4600                          
C988A98886FFFF6FFFF6FFFF6FFFF6FFFF6FFFF6FFFF6FFFF6FFFF                          
695527135B4942B4729B4930B4889B4777B4799B4302B4567B4600                          
Allokeret,6784,7272,7079,7112,7232,7210,7707,7441,7408                          
C9999898A6FFFF6FFFF6FFFF6FFFF6FFFF6FFFF6FFFF6FFFF6FFFF                          
133625953B6784B7272B7079B7112B7232B7210B7707B7441B7408                          

I suspect that something happens when i FTP the file to our server on the local network, causing the extra blank line to appear.

The javascript i use:

    $.get("../classic_3270/KMDprod1/INFO.CPU.REPORT.MFDISK" + kunde + ".txt", function (data) {
        var lines = data.split('\n');
        $.each(lines, function(lineNo, line) {
            var items = line.split(',');
            if (lineNo == 0) {
                $.each(items, function(itemNo, item) {
                    if (itemNo > 0) graph.xAxis.categories.push(item);
                })
                ;
            }

(This code will extract data from the CSV file, and push it to a graphing software)

Is there any way that i could modify the above, as to make it ignore the last line, which is empty?

I have to be honest and admit that im no expert on javascript, and google haven't been able to help me this time.

I hope i have have supplied sufficient information. Thanks in advance.

Upvotes: 0

Views: 2070

Answers (2)

dandavis
dandavis

Reputation: 16726

$.get("../classic_3270/KMDprod1/INFO.CPU.REPORT.MFDISK" + kunde + ".txt", function (data) {
        var lines = data.replace(/^\s+|\s+$/g,"").split('\n');
        $.each(lines, function(lineNo, line) {
            var items = line.split(',');
            if (lineNo == 0) {
                $.each(items, function(itemNo, item) {
                    if (itemNo > 0) graph.xAxis.categories.push(item);
                })
                ;
            }
  ...

i took out the trim() and used regexp for ie8 compat...

Upvotes: 1

MaxArt
MaxArt

Reputation: 22637

Try filtering the lines:

var lines = $.grep(data.split('\n'), Boolean);

This also deletes all the empty lines, not just the last one.

But if you know that you'll have an empty last line, why don't you go for something simpler like data.split('\n').slice(0, -1)?

Upvotes: 1

Related Questions