JAVASCRIPT read a TXT file

Asked

Viewed 1,173 times

2

I made a command in linux that saves in a txt the output of the terminal from time to time, in case the output of the command free -m, I wanted to read that file. txt so that javascript would update the values periodically without having to reload the page, in case the return of the terminal would be something like:

       total    used    free    shared    buff/cache    available
Mem:    2004     998     155       231           850          511
Swap:   4000       0    4000

Wanted Script to pick up saved texts in a type array

Array[0] = "total used free Shared Buff available"
Array[1] = "Mem: 2004 998 155 231 850 511"
Array[2] = "Swap: 4000 0 4000"

'Cause after that I’d use the Array[1]. split(" ") to separate into another array containing each of the memory values and the Array[2]. split(" ") containing each of the swap values

1 answer

1


Using the fetch you can read the file.

fetch('arquivo.txt')
  .then(response => response.text())
  .then(text => {
    const array = text.split("\n");
    console.log(array);
  })

Here an example., see the array on the console.

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.