Open text file and handle by line

Asked

Viewed 919 times

0

I’m starting the first steps in and I am needing to first get a text file open it and filter by row where each line is separated by | and I’m gonna make a if to show a certain line

Example:

Content of the text file:

|C100|1|0||55|05|1|000002397||||||||||||||||||||||
|C100|1|0||55|05|1|000002399||||||||||||||||||||||
|C100|1|0||55|05|1|000002425||||||||||||||||||||||
|C100|1|0||55|05|1|000002371||||||||||||||||||||||
|C400|2D|ECF|asdasda2330|002|
|C405|02012012|3|1036|116526|6001060,09|5693,36|
|C420|DT|151,78|||
|C420|01T1700|429,50|01||
|C420|F1|5112,08|||
|C460|2D|00|116397|02012012|25,00|0,16|0,75|||
|C470|0000008556|2,000|0|RL|25,00|060|5403|0|0,16|0,75|
|C460|2D|00|116398|02012012|27,67|0,19|0,83|||
|C470|0000015177|1,000|0|RL|5,60|060|5403|0|0,04|0,17|
|C470|0000022346|1,000|0|RL|5,60|060|5403|0|0,04|0,17|
|C470|0000025882|1,000|0|PC|6,09|060|5403|0|0,04|0,18|
|C470|0000025885|2,000|0|PC|10,38|060|5403|0|0,07|0,31|
|C460|2D|00|116399|02012012|19,90|0,13|0,60|||
|C470|0000000247|3,000|0|RLO|10,20|060|5403|0|0,07|0,31|
|C470|0000020634|1,000|0|PC|9,70|060|5403|0|0,06|0,29|
|C460|2D|00|116400|02012012|10,52|0,07|0,32|||
|C470|0000004762|1,000|0|CAT|1,12|060|5403|0|0,01|0,03|
|C470|0000008873|1,000|0|PC|1,94|060|5403|0|0,01|0,06|
|C470|0000016902|1,000|0|PC|3,68|060|5403|0|0,02|0,11|
|C470|0000023287|1,000|0|PC|1,26|060|5403|0|0,01|0,04|
|C470|0000023303|1,000|0|PC|1,26|060|5403|0|0,01|0,04|
|C470|0000023308|1,000|0|PC|1,26|060|5403|0|0,01|0,04|
|C460|2D|00|116401|02012012|5,00|0,04|0,16|||
|C470|0000009772|1,000|0|UN|1,25|000|5102|17,00|0,01|0,04|
|C470|0000009789|1,000|0|UN|1,25|000|5102|17,00|0,01|0,04|
|C470|0000009826|1,000|0|UN|1,25|000|5102|17,00|0,01|0,04|
|C470|0000009842|1,000|0|UN|1,25|000|5102|17,00|0,01|0,04|

I first need to open the file and store all its contents and then do kind a explode of each line by putting in one array done that I’ll make a if linha[1] == "C460" and shows the entire contents of that line

package main

import (
    "fmt"
    "io/ioutil"
    "strings"
)


func main() {
    conteudo,_ := ioutil.ReadFile("sped.txt")
    linhas := strings.Split(string(conteudo),"|")
    for _,v := range (linhas){
        if v == "C100"{
            fmt.Println(v) // preciso mostra a linha toda do registro C100
        }
    }
    fmt.Println(linhas[1])

}
  • If you want to take the line, your strings.Split should consider the separator \n and not |

1 answer

1


  1. Do not use split, it removes the character passed in the second parameter and returns a string from the content result.
  2. When you made one for you compared if the whole file is equal to C100, but you need to compare column by column of each row.

I did with regular expression, see if this is it, in this case just change the name by the content example: C100,C470

package main

import (
    "fmt"
    "io/ioutil"
    "regexp"
    "strings"
)

func main() {
    // abrindo arquivo sped.txt
    conteudo, _ := ioutil.ReadFile("sped.txt")

    nome := "C100" // nome que irá procurar...

    // expressão regular que verifica se o nome existe, se existe ele pega todo conteúdo
    r, err := regexp.Compile("(?P<name>" + nome + ").+")

    // verifica se houve algum erro ao pegar o conteudo
    if err != nil {
        fmt.Printf("Ocorreu um erro: %v", err)
    }

    // cria um slice de string para cada linha de string encontrada
    x := strings.Fields(string(conteudo))

    // percorre todo o slice de string
    for _, a := range x {
        fmt.Println(r.FindString(string(a))) // retorna apenas as linhas pelo nome escolhido
    }
}

Link to the complete code, https://play.golang.org/p/U_dniiFOeJ but it won’t work because inside the server you don’t have the sped.txt file, so you copy and run locally.

Browser other questions tagged

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