From 9c06e2a8f77c7f61938fbda091a9cb09aaab221c Mon Sep 17 00:00:00 2001 From: Ethan Brown Date: Tue, 11 Jun 2013 09:39:39 -0700 Subject: [PATCH] Remove empty fields after split to ensure fields match what's expected. --- jawk.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/jawk.js b/jawk.js index fcf6471..09afd3d 100755 --- a/jawk.js +++ b/jawk.js @@ -92,6 +92,17 @@ function executeAction( rule, context ) { } } +// taken from http://stackoverflow.com/questions/281264/remove-empty-elements-from-an-array-in-javascript +Array.prototype.clean = function(deleteValue) { + for (var i = 0; i < this.length; i++) { + if (this[i] == deleteValue) { + this.splice(i, 1); + i--; + } + } + return this; +}; + if( begin ) executeAction( begin, jawkContext ); readlines( inputFname, 'utf8', function(line) { @@ -100,7 +111,7 @@ readlines( inputFname, 'utf8', function(line) { if( line.match( rules[i].regex ) ) { jawkContext.$0 = line; // build record fields - var fields = line.split( new RegExp( jawkContext.RS ) ); + var fields = line.split( new RegExp( jawkContext.RS ) ).clean(''); jawkContext.NR = fields.length; var j; for( j=0; j