commit 368fb6407dfe182613bc1842400030fd3a158180 Author: sneak Date: Wed May 22 14:36:15 2024 -0700 initial diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dd686ca --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +field diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..9130c17 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module sneak.berlin/go/field + +go 1.22.2 diff --git a/main.go b/main.go new file mode 100644 index 0000000..68ddde3 --- /dev/null +++ b/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + if len(os.Args) < 2 { + fmt.Fprintf(os.Stderr, "usage: %s ", os.Args[0]) + os.Exit(1) + } + + fieldNum, err := strconv.Atoi(os.Args[1]) + if err != nil || fieldNum < 1 { + fmt.Fprintln(os.Stderr, "invalid field number:", os.Args[1]) + os.Exit(1) + } + + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + line := scanner.Text() + fields := strings.Fields(line) + if len(fields) >= fieldNum { + fmt.Println(fields[fieldNum-1]) + } + } + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, "error reading input:", err) + } +}