-
Notifications
You must be signed in to change notification settings - Fork 0
/
filterlines-ab.go
54 lines (47 loc) · 1.13 KB
/
filterlines-ab.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
The program removes all lines from file1 that also appear in file2 and outputs the remaining lines.
It’s useful for filtering out unwanted lines from a file based on a list of lines provided in another file.
*/
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: filterlines file1 file2")
return
}
file1, err := os.Open(os.Args[1])
if err != nil {
fmt.Fprintf(os.Stderr, "error opening file1: %v\n", err)
return
}
defer file1.Close()
file2, err := os.Open(os.Args[2])
if err != nil {
fmt.Fprintf(os.Stderr, "error opening file2: %v\n", err)
return
}
defer file2.Close()
linesToRemove := make(map[string]bool)
scanner := bufio.NewScanner(file2)
for scanner.Scan() {
line := scanner.Text()
linesToRemove[line] = true
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "error reading file2: %v\n", err)
}
scanner = bufio.NewScanner(file1)
for scanner.Scan() {
line := scanner.Text()
if !linesToRemove[line] {
fmt.Println(line)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "error reading file1: %v\n", err)
}
}