Open text file and handle by line

0

I'm starting the first step in and I'm needing it first get a text file to open it and filter by line where each line is separated by | and I will make a if to show certain line

Example:

Text file content:

|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 need to first open the file and store all its contents and then type an explode of each line by putting in an array done this I'll make a if linha[1] == "C460" and show the whole content 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])

}
    
asked by anonymous 14.02.2017 / 03:56

1 answer

1
  • Do not use split, it removes the character passed in the second parameter and returns a string from the result of the content.
  • When you made a for you compared if the whole file is equal to C100, but you need to compare column by column of each line.
  • I made regular expression, see if it is, in this case just change the name for 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 full code, link however will not work because inside the server does not have the sped.txt file, then you copy and execute locally.

        
    26.04.2017 / 04:03