2
0
Fork 0
FLIK/fix-movie-list/txt-to-md.go

59 lines
1.1 KiB
Go
Raw Normal View History

2023-05-29 19:05:56 -04:00
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
func main() {
// Open input file
file, err := os.Open("movie-list.txt")
if err != nil {
panic(err)
}
defer file.Close()
// Create output file
output, err := os.Create("movies.md")
if err != nil {
panic(err)
}
defer output.Close()
// Regex to match movie title, year, and director
r := regexp.MustCompile(`(.*?)(?:\s|\()(\d{4})(?:\)|\s|$).*`)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Parse line using regex
matches := r.FindStringSubmatch(line)
if len(matches) != 3 {
// Skip lines that don't match our expected format
continue
}
// Remove leading/trailing whitespace and adjust year format
movieTitle := strings.TrimSpace(matches[1])
year := matches[2]
// Output fixed movie title and year to terminal
fmt.Printf("Fixed: %s (%s)\n", movieTitle, year)
// Format movie title and year, write to output file
_, err = fmt.Fprintf(output, "%s (%s)\n", movieTitle, year)
if err != nil {
panic(err)
}
}
// Check for errors from scanner
if err := scanner.Err(); err != nil {
panic(err)
}
}