Пример #1
0
Файл: fld.go Проект: gaal/shstat
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "%s prints selected input columns.\n", os.Args[0])
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
	}
	flag.Parse()

	keys := flag.Args()
	if *keyspec != "" {
		if len(keys) > 0 {
			fmt.Fprintln(os.Stderr, "usage: fld KEY... or fld -k=KEYS, but not both")
			os.Exit(1)
		}
		keys = strings.Split(*keyspec, ",")
	}

	ifs := regexp.MustCompile(*inDelim)
	*outDelim = strings.Replace(*outDelim, `\t`, "\t", -1) // silent magic for now, figure it out later.
	idx, err := internal.AtoiList(keys)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if err := fld(os.Stdin, os.Stdout, ifs, *outDelim, idx); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
Пример #2
0
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "%s sums input by columns (or the first input column by default).\n", os.Args[0])
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
	}
	flag.Parse()

	ifs := regexp.MustCompile(*inDelim)
	*outDelim = strings.Replace(*outDelim, `\t`, "\t", -1) // silent magic for now, figure it out later.
	idx, err := internal.AtoiList(flag.Args())
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if len(idx) == 0 {
		idx = []int{1}
	}
	if err := tally(os.Stdin, os.Stdout, ifs, *outDelim, idx); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}
Пример #3
0
func main() {
	internal.SetUsage(
		`hist computes a histogram on its input.

  # Show a histogram of words in input (uses naive tokenization)
  $ hist -words < corpus.txt
     203 of        +
     771 a         ++++
    4025 the       +++++++++++++++++++++++
         ...

  # Same, on input that happens to have a word per line. Use log scale in graph.
  $ hist -scale=log < corpus_tokenized.txt
     203 of        +++++++++++++++
     771 a         ++++++++++++++++++
    4025 the       +++++++++++++++++++++++
         ...

  # Show a histogram of the 2nd field, using the 3rd field as weight.
  $ cat mydata
  orange vest 42
  blue vest 5
  white jumpsuit 2

  $ hist -k 2 -w 3 -graph=false < mydata
       2 jumpsuit
      47 vest
         ...

  # You can set -ofs=, for CSV output, or \t for TSV.
  $ hist -k -w 3 -graph -ofs=\\t

Output order is by increasing counts. To change order, pipe through sort and
possibly use its -n and -k flags.

Currently only integer data is supported.`)
	flag.Parse()

	*outDelim = strings.Replace(*outDelim, `\t`, "\t", -1)
	*inDelim = strings.Replace(*inDelim, `\t`, "\t", -1)
	ifs := regexp.MustCompile(*inDelim)
	keysstr := strings.Split(*keyspec, ",")
	if len(keysstr) == 1 && keysstr[0] == "" {
		keysstr = nil
	}
	keys, err := internal.AtoiList(keysstr)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if *words && (len(keys) > 0 || *weightspec != 0) {
		fmt.Fprintln(os.Stderr, "--words cannot be used with -k or -w")
		os.Exit(1)
	}

	var gtype gType
	switch *scale {
	case "none":
		gtype = gNone
	case "log":
		gtype = gLog
	case "linear":
		gtype = gLinear
	default:
		fmt.Fprintf(os.Stderr, "bad -scale=%q\n", *scale)
		os.Exit(1)
	}
	tw := termWidth()
	if tw < 20 {
		tw = 20
	}
	h := &histogrammer{
		keys:      keys,
		weightCol: *weightspec,
		words:     *words,
		gt:        gtype,
		ifs:       ifs,
		ofs:       *outDelim,
		termWidth: tw,
		snip:      *snippet,
	}
	kc, err := h.hist(os.Stdin)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	if err = h.printHist(os.Stdout, kc); err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
}