Handling a txt file with javascript

Asked

Viewed 908 times

-1

Well, I wanted through a txt file, to transform the lines into arrays (if it is better) to search within it a sequence with 9 numbers, and then link to an image. Obs: within the txt’s that will be read there will be more than one sequence with 9 numbers. can do this with Ode or other technology

const readline = require('readline')
const fs = require('fs')
const readable = fs.createReadStream('Marcas2507.txt')
var rl = readline.createInterface({
    input : readable,
    output: process.stdout

})
rl.on('line',function(line){
   console.log(line) // Não sei como transformar o txt em um array para identificar uma sequencia de 9 numeros nele
})
  • You can give examples of what the .txt?

  • 916427416 Publication of opposition registration request (formal examination completed) Holder: restricted Date of filing: 12/13/2018 Presentation: restricted Nature: restricted Nominative element: restricted CFE: 27.5.1 NCL(11): 41 Specification: restricted; Proxy: restricted

  • after the sequence of 9 numbers(process number) there is a line break

  • And what do you want to do is extract these 9 digits each time they appear? Is the structure always the same? (ie: numbers > line break > text | and the same repeated N times)

  • I want to link these 9 numbers to an image (as if it were an image naming through the process number (the 9 numbers in the case). And yes there is always a line break every time these 9 digits appear

  • You could read the whole file at once and do something like this: https://jsfiddle.net/Sergio_fiddle/btLpn6x1/ is what you’re looking for?

  • How would I identify this 9-number sequence of this JSON to switch to an image or place within an array? only the numbers

  • I still don’t fully understand what you want to do... have you seen my jsFiddle? there is created an array as you look. That’s not how you thought?

  • I’m sorry I didn’t make it clear, here’s the deal. i want to develop a software that reads a complete txt file and extracts from it only the processes(sequence of 9 numbers) and store this data somehow, so that then I link each process with a specific image. in your example you have stored all the information within the array. i just want to store the process number.

  • https://jsfiddle.net/Sergio_fiddle/btLpn6x1/4/ ?

  • perfect, thank you very much!!

Show 6 more comments

1 answer

1


If you want to extract the numbers you can do so, assuming that the file formatting is consistent:

const txt = `
916427416 
Publicação de pedido de registro para oposição (exame formal concluído) Titular: restrito Data de depósito: 13/12/2018 Apresentação: restrito Natureza: restrito Elemento nominativo: restrito CFE: 27.5.1 NCL(11): 41 Especificação: restrito; Procurador: restrito

846483729 
Publicação de pedido de registro para oposição (exame formal concluído) Titular: restrito Data de depósito: 13/12/2018 Apresentação: restrito Natureza: restrito Elemento nominativo: restrito CFE: 27.5.1 NCL(11): 41 Especificação: restrito; Procurador: restrito
`;

const data = txt
  .split(/\n/g)
  .map(str => str.trim())
  .filter(str => {
    if (!Boolean(str)) return false;
    const match = str.match(/^\d{9}$/);
    return match && match[0];
  });
console.log(JSON.stringify(data, null, 4));

In practice you separate the file line by line and then extract only the lines that start and end with numbers, exactly 9.

Browser other questions tagged

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