-
Notifications
You must be signed in to change notification settings - Fork 0
/
qcat.go
43 lines (39 loc) · 878 Bytes
/
qcat.go
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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
)
var leftDelim = flag.String("l", `"`, "left delimiter")
var rightDelim = flag.String("r", `"`, "right delimiter")
var escape = flag.Bool("escape", true, "escape all quotes within each line")
var trim = flag.Bool("trim", true, "trim all whitespace from each line before quoting")
func main() {
flag.Parse()
if flag.NArg() == 0 || flag.Arg(1) == "-" {
quotecat(os.Stdin)
} else {
for _, fn := range flag.Args() {
file, err := os.Open(fn)
if err != nil {
panic(err)
}
quotecat(file)
}
}
}
func quotecat(file *os.File) {
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if *escape {
line = strings.Replace(line, `"`, `\"`, -1)
}
if *trim {
line = strings.Trim(line, " \r\n\t")
}
fmt.Printf("%s%s%s\n", *leftDelim, line, *rightDelim)
}
}