-6

I have a big data in this format and I want to extract some information from it:

[**] [122:1:0] (portscan) TCP Portscan [**]
[Priority: 3] 
04/13-07:54:00.585009 192.168.2.136 -> 192.168.1.14
PROTO:255 TTL:0 TOS:0x0 ID:62399 IpLen:20 DgmLen:165 DF

How I should read this text file in javascript?

  • 1
    Server-side (Node) JS, or in the browser? – nnnnnn Feb 28 '17 at 07:01
  • Possible duplicate of [Javascript - read local text file](http://stackoverflow.com/questions/14446447/javascript-read-local-text-file) – Darshak Feb 28 '17 at 07:01
  • Are you asking how to parse this text, or how to load this text into JavaScript? If load, then are you asking how to do it in the browser, or on server-side? – Bemmu Feb 28 '17 at 07:02
  • It depends on (a) what tool you are using to run the JS and (b) where you want to read the file from (local file, making an http request, user posting it over http) – Quentin Feb 28 '17 at 07:03

1 Answers1

0

OK This is my solution to extract the information

function readTextFile(file_name)
{
    var file = new XMLHttpRequest();
    file.open("GET", file_name, false);
    file.onreadystatechange = function ()
    {
        if(file.readyState === 4)
        {
            if(file.status === 200 || file.status == 0)
            {
                allText = file.responseText;
                document.write(allText+"<BR>");
            }
        }
    }
    file.send(null);
}
function processTextValue()
{
 var in_arr = ['PROTO', 'TTL', 'TOS', 'ID', 'IpLen', 'DgmLen'];
 for(var i=0; i<in_arr.length; i++)
 {
  var c = in_arr[i];
  var n = allText.indexOf(c);
  var m = allText.indexOf(" ", n);
  n = n+c.length+1;
  document.write(c+": "+allText.substr(n, m-n)+"<BR>");
 }
}

readTextFile('your_file.txt');
processTextValue();

This is the result:

PROTO: 255
TTL: 0
TOS: 0x0
ID: 62399
IpLen: 20
DgmLen: 165

Chien_Khmt
  • 257
  • 2
  • 8