Shan Peiris
Shan Peiris

Reputation: 183

Read file line by line and separate each line by spaces by javascript

I wrote the code to read the file using HTML5 Filereader API .reading line by line is done.but now what i want to do is get separate data from each line.

If i read a file like below.

2016-8-11 23:13:27  hdhtdht hththth
2016-8-11 23:13:27  edhdhdh  ehdhdhd= dhdhd
2016-8-11 23:13:27  dfgdfgdg eagsdrgergared ergargge
2016-8-11 23:13:27  dgbdfhgb gdhdhgddh
2016-8-11 23:13:27  ggggggggggggg gtrrrrrrrrr

i want to get separately time , date and all other details to one attribute.

{
"_id" : ObjectId("5926c4581d3e69c01f32b074"),
"dat" : "2016-8-11",
"details" : "hdhtdht hththth",
"tim" : "23:13:27"
}

so for each line there will be three attributes.i attempted to split using spit() method.but it separate the contents as well. can anyone suggest a way??

this is javascript fuction i wrote so far.

$scope.addFileContents= function(readDetails)
{
//read line by line
 var lines = readDetails.split('\n');
 for(var line = 0; line < lines.length; line++){
 //separate by spaces
 var linesSpace = lines[line].split(' ');

  var event = {
    dat: linesSpace[0],
    tim: linesSpace[1],
    details: linesSpace[2],
    space:linesSpace[3]
  };  
  Event.ReadUploadFile(event)
      .success(function () {
          $scope.status = 'Reading the selected file';
          $scope.fileDetails.push(event);
      }).
      error(function (error) {
          $scope.status = 'Unable to insert data: ' + error.message;
      });
}

}

Thanks.

Upvotes: 2

Views: 2522

Answers (3)

zer00ne
zer00ne

Reputation: 43930

Used regex to separate the strings and Array.prototype.map() for event so it's not an object but an array. Demo 1 is how I believe it'll fit into your function. Demo 2 is the function actually functioning (I don't understand the rest of your code.)

Details are commented in Demo 2

Demo 1

var readDetails = `2016-8-11 23:13:27  hdhtdht hththth
2016-8-11 23:13:27  edhdhdh  ehdhdhd= dhdhd
2016-8-11 23:13:27  dfgdfgdg eagsdrgergared ergargge
2016-8-11 23:13:27  dgbdfhgb gdhdhgddh
2016-8-11 23:13:27  ggggggggggggg gtrrrrrrrrr`;

$scope.addFileContents = function(readDetails) {
  var rgx = /(\d{4}-\d?-\d{1,2})\s(\d\d:\d\d:\d\d)\s\s(.*)/g;
  
  var cap = `$1|$2|$3`;
  
  var lines = readDetails.split('\n');
  
  var event = lines.map(function(line, idx) {
    var data = (line.replace(rgx, cap)).split('|');
    var frag = {
      date: data[0],
      time: data[1],
      details: data[2]
    };
    return frag;
  });
  
  console.log(event);

  Event.ReadUploadFile(event)
    .success(function() {
      $scope.status = 'Reading the selected file';
      $scope.fileDetails.push(event);
    }).
  error(function(error) {
    $scope.status = 'Unable to insert data: ' + error.message;
  });
}

Demo 2

var readDetails = `2016-8-11 23:13:27  hdhtdht hththth
2016-8-11 23:13:27  edhdhdh  ehdhdhd= dhdhd
2016-8-11 23:13:27  dfgdfgdg eagsdrgergared ergargge
2016-8-11 23:13:27  dgbdfhgb gdhdhgddh
2016-8-11 23:13:27  ggggggggggggg gtrrrrrrrrr`;

/* Sloppy regex:
{4 digits}-{ 1 or 2 digits}-{1 to 2 digits}{space}
{2 digits}:{2 digits}:{2 digits}{2 spaces}{Anything until end of line}
*/
var rgx = /(\d{4}-\d?-\d{1,2})\s(\d\d:\d\d:\d\d)\s\s(.*)/g;

/* 3 capture groups delimited by a "|"
|| If you need to have any repetitive text included in each line
|| this is a good place to insert it.
*/
var cap = `$1|$2|$3`;

// An array of strings
var lines = readDetails.split('\n');

// map() each string of array to...
var event = lines.map(function(line, idx) {

  /* replace() each match on string so that it's
  || split() 3 ways delimited by the "|"
  */
  var data = (line.replace(rgx, cap)).split('|');

  /* The 3 new strings are assigned to a property
  || of an object literal
  */
  var frag = {
    date: data[0],
    time: data[1],
    details: data[2]
  };
  /* Each object literal is then returned as an
  || element of an array
  */
  return frag;
});
console.log(event);

Upvotes: 1

Phylogenesis
Phylogenesis

Reputation: 7880

Use the following algorithm:

  1. Split the string into separate words
  2. Shift the first 2 items off the resultant array into variables
  3. Join the remaining items back into a single string

Example:

var data    = lines[line].split(' ');
var date    = data.shift();
var time    = data.shift();
var details = data.join(' ');

Upvotes: 1

Ovidiu Dolha
Ovidiu Dolha

Reputation: 5413

A quick way would be to just join back last elements (after date and time) of the result from split (this is assuming the layout is always the same)

var linesSpace = lines[line].split(' ');

var event = {
    dat: linesSpace[0],
    tim: linesSpace[1],
    details: linesSpace.slice(2).join('')
};  

Upvotes: 1

Related Questions