-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvdiff.go
More file actions
154 lines (124 loc) · 3.71 KB
/
csvdiff.go
File metadata and controls
154 lines (124 loc) · 3.71 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"bufio"
"encoding/csv"
"fmt"
"log"
"os"
"strings"
"time"
)
func main() {
fmt.Println("╔═══════════════════════════════════════════════════════╗")
fmt.Println("║ CSV-DIFF ║")
fmt.Println("╠═══════════════════════════════════════════════════════╣")
fmt.Println("║ Das Programm erwartet zwei csv Dateien als Eingabe. ║")
fmt.Println("║ Die Differenz wird im selben Verzeichnis gespeichert. ║")
fmt.Println("╚═══════════════════════════════════════════════════════╝")
reader := bufio.NewReader(os.Stdin)
file1, err := getFilePath(reader, "Erste csv-Datei: ")
if err != nil {
log.Fatal(err)
}
file2, err := getFilePath(reader, "Zweite csv-Datei: ")
if err != nil {
log.Fatal(err)
}
outputFile := "output_" + time.Now().Format("2006-01-02_15-04-05") + ".csv"
records1, err := readCSV(file1)
if err != nil {
log.Fatal(err)
}
records2, err := readCSV(file2)
if err != nil {
log.Fatal(err)
}
err = writeCSV(outputFile, findUniqueRecords(records1, records2))
if err != nil {
log.Fatal(err)
}
fmt.Println("Vergleich abgeschlossen.\nErgebnisse in", outputFile, "gespeichert.\n")
// Damit das Fenster beim Ausführen über Explorer offen bleibt.
fmt.Println("\nNow please press the Any-Key and get me some coffee!")
reader.ReadString('\n')
os.Exit(0)
}
func getFilePath(reader *bufio.Reader, prompt string) (string, error) {
fmt.Print(prompt)
path, err := reader.ReadString('\n')
if err != nil {
return "", err
}
path = strings.TrimSpace(path)
if path == "" {
return "", fmt.Errorf("Bitte geben Sie einen gültigen Dateipfad ein\n")
}
if !isValidPath(path) {
return "", fmt.Errorf("Der angegebene Pfad ist ungültig\n")
}
return path, nil
}
func isValidPath(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func readCSV(filename string) ([][]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("Fehler beim Öffnen von %s: %v", filename, err)
}
defer file.Close()
reader := csv.NewReader(file)
records, err := reader.ReadAll()
if err != nil {
return nil, fmt.Errorf("Fehler beim Lesen von %s: %v", filename, err)
}
return records, nil
}
func findUniqueRecords(records1, records2 [][]string) [][]string {
var uniqueRecords [][]string
for _, record := range records1 {
if !containsRecord(records2, record) {
uniqueRecords = append(uniqueRecords, record)
}
}
for _, record := range records2 {
if !containsRecord(records1, record) {
uniqueRecords = append(uniqueRecords, record)
}
}
return uniqueRecords
}
func containsRecord(records [][]string, record []string) bool {
for _, r := range records {
if equalRecords(r, record) {
return true
}
}
return false
}
func equalRecords(record1, record2 []string) bool {
if len(record1) != len(record2) {
return false
}
for i, value := range record1 {
if value != record2[i] {
return false
}
}
return true
}
func writeCSV(filename string, records [][]string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("Fehler beim Erstellen von %s: %v", filename, err)
}
defer file.Close()
writer := csv.NewWriter(file)
writer.UseCRLF = false
err = writer.WriteAll(records)
if err != nil {
return fmt.Errorf("Fehler beim Schreiben in %s: %v", filename, err)
}
return nil
}