I'm very new to Golang
and I'm having some issues on trying to find
and print
all the lines in a file which contain a certain same value
.
My file is structured like the following:
index text
index text
.
.
.
index text
Where index
is ALWAYS 6 digits long
and text
is ALWAYS 16 digits long
.
I need to
find
andtext
value.
That's what I tried so far:
func main() {
//Array to contain common texts
found := make([]string, 6)
r, _ := os.Open("store.txt")
scanner := bufio.NewScanner(r)
//Splits in words
scanner.Split(bufio.ScanWords)
//Loop over all Words in the file
for scanner.Scan() {
line := scanner.Text()
//If the current line is 16 digits long
if(utf8.RuneCountInString(line) == 16){
currLine := line
//Search in the same files all the 16 digits long texts and
for scanner.Scan(){
searchLine := scanner.Text()
//If a same text is found
if(utf8.RuneCountInString(searchLine) == 16){
//Append it to found array
if(currLine == searchLine){
found = append(found, currLine)
}
}
}
}
}
//Print found Array
fmt.Println(found)
//Close File
r.Close()
}
Then, I would like to use found
to print
all the lines
which match the current found[i-element]
.
The code above works only for the very first step.
For instance, If in my file, at the very first line it gets 1234567890123456
(e.g. from index 1) then it checks and appends only one time, it does not loop for all the lines (for the remaining n-1 words).
How can I fix the first issue?
Do you think adding the duplicate
texts
in anArray
and then print the matching lines based on it is a bad idea?
Thanks in advance.