initial
This commit is contained in:
commit
368fb6407d
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
field
|
34
main.go
Normal file
34
main.go
Normal file
@ -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 <field-number>", 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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user