And process any number of files.

master
Zed A. Shaw 2 weeks ago
parent e978ca4e7f
commit 1f8a28c041
  1. 31
      cat/main.go

@ -17,7 +17,7 @@ func Fail(err error, format string, v ...any) {
type Opts struct {
Number bool
Squeeze bool
Filename string
Filenames []string
}
func parse_opts() (Opts) {
@ -32,28 +32,31 @@ func parse_opts() (Opts) {
os.Exit(1)
}
opts.Filename = flag.Arg(0)
opts.Filenames = flag.Args()
return opts
}
func main() {
opts := parse_opts()
in_file, err := os.ReadFile(opts.Filename)
if err != nil { Fail(err, "cannot open %s:", opts.Filename) }
for _, filename := range opts.Filenames {
in_file, err := os.ReadFile(filename)
if(opts.Number) {
count := 1
for line := range strings.Lines(string(in_file)) {
if opts.Squeeze && len(line) <= 1 {
continue
}
if err != nil { Fail(err, "cannot open %s:", filename) }
if(opts.Number) {
count := 1
for line := range strings.Lines(string(in_file)) {
if opts.Squeeze && len(line) <= 1 {
continue
}
fmt.Printf("%0.4d: %s", count, line)
count++
fmt.Printf("%0.4d: %s", count, line)
count++
}
} else {
fmt.Print(string(in_file))
}
} else {
fmt.Print(string(in_file))
}
}

Loading…
Cancel
Save