查找重复行操作
-
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
-
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
-
-
// See page 8.
-
//!+
-
-
// Dup1 prints the text of each line that appears more than
-
// once in the standard input, preceded by its count.
-
package main
-
-
import (
-
"bufio"
-
"fmt"
-
"os"
-
)
-
-
func main() {
-
counts := make(map[string]int)
-
input := bufio.NewScanner(os.Stdin)
-
for input.Scan() {
-
counts[input.Text()]++
-
}
-
// NOTE: ignoring potential errors from input.Err()
-
for line, n := range counts {
-
if n > 1 {
-
fmt.Printf("%d\t%s\n", n, line)
-
}
-
}
-
}
-
-
//!-
---------------------------------------------------------------------------
-
// ch1 project main.go
-
package main
-
-
import (
-
"bufio"
-
"bytes"
-
"fmt"
-
"log"
-
"os"
-
)
-
-
var (
-
buf bytes.Buffer
-
logger = log.New(&buf, "INFO: ", log.Lshortfile)
-
)
-
-
func info_log(info string) {
-
logger.Output(2, info)
-
fmt.Print(&buf)
-
}
-
-
func main() {
-
var f *os.File
-
var err error
-
if len(os.Args) < 2 {
-
info_log("args err")
-
os.Exit(-1)
-
}
-
f, err = os.Open(os.Args[1])
-
if err != nil {
-
info_log("open file error")
-
os.Exit(-1)
-
}
-
-
input := bufio.NewScanner(f)
-
var counter = make(map[string]int)
-
for input.Scan() {
-
counter[input.Text()]++
-
}
-
-
for text, n := range counter {
-
fmt.Printf("%d: %s\n", n, text)
-
}
-
-
}
结果输出:
[root@hadoop1 ch1]# ./echo_my aa.txt
2: hello world
1: good idea
1: this is good
1: hello world this is
[root@hadoop1 ch1]#
---------------------------------------------------------------------------
-
package main
-
-
import (
-
"bufio"
-
"fmt"
-
"os"
-
)
-
-
func main() {
-
counts := make(map[string]int)
-
files := os.Args[1:]
-
if len(files) == 0 {
-
countLines(os.Stdin, counts)
-
} else {
-
for _, arg := range files {
-
f, err := os.Open(arg)
-
if err != nil {
-
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
-
continue
-
}
-
countLines(f, counts)
-
f.Close()
-
}
-
}
-
for line, n := range counts {
-
if n > 1 {
-
fmt.Printf("%d\t%s\n", n, line)
-
}
-
}
-
}
-
-
func countLines(f *os.File, counts map[string]int) {
-
input := bufio.NewScanner(f)
-
for input.Scan() {
-
counts[input.Text()]++
-
}
-
// NOTE: ignoring potential errors from input.Err()
-
}
-
-
//!-
结果输出:
[root@hadoop1 ch1]# ./echo_my aa.txt
2 hello world
[root@hadoop1 ch1]#
--------------------------------------------------------------------------------
-
package main
-
-
import (
-
"fmt"
-
"io/ioutil"
-
"os"
-
"strings"
-
)
-
-
func main() {
-
counts := make(map[string]int)
-
for _, filename := range os.Args[1:] {
-
data, err := ioutil.ReadFile(filename)
-
if err != nil {
-
fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
-
continue
-
}
-
for _, line := range strings.Split(string(data), "\n") {
-
counts[line]++
-
}
-
}
-
for line, n := range counts {
-
if n > 1 {
-
fmt.Printf("%d\t%s\n", n, line)
-
}
-
}
-
}
-
-
//!-
输出结果:
[root@hadoop1 ch1]# ./echo_my aa.txt
2 hello world
[root@hadoop1 ch1]#
阅读(574) | 评论(0) | 转发(0) |