Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334663
  • 博文数量: 214
  • 博客积分: 4258
  • 博客等级: 上校
  • 技术积分: 2021
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-02 09:16
个人简介

http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net

文章分类

全部博文(214)

文章存档

2018年(16)

2015年(1)

2014年(2)

2012年(22)

2011年(173)

分类: C/C++

2018-01-30 20:41:53

查找重复行操作

点击(此处)折叠或打开

  1. // Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
  2. // License: https://creativecommons.org/licenses/by-nc-sa/4.0/

  3. // See page 8.
  4. //!+

  5. // Dup1 prints the text of each line that appears more than
  6. // once in the standard input, preceded by its count.
  7. package main

  8. import (
  9.     "bufio"
  10.     "fmt"
  11.     "os"
  12. )

  13. func main() {
  14.     counts := make(map[string]int)
  15.     input := bufio.NewScanner(os.Stdin)
  16.     for input.Scan() {
  17.         counts[input.Text()]++
  18.     }
  19.     // NOTE: ignoring potential errors from input.Err()
  20.     for line, n := range counts {
  21.         if n > 1 {
  22.             fmt.Printf("%d\t%s\n", n, line)
  23.         }
  24.     }
  25. }

  26. //!-
---------------------------------------------------------------------------

点击(此处)折叠或打开

  1. // ch1 project main.go
  2. package main

  3. import (
  4.     "bufio"
  5.     "bytes"
  6.     "fmt"
  7.     "log"
  8.     "os"
  9. )

  10. var (
  11.     buf bytes.Buffer
  12.     logger = log.New(&buf, "INFO: ", log.Lshortfile)
  13. )

  14. func info_log(info string) {
  15.     logger.Output(2, info)
  16.     fmt.Print(&buf)
  17. }

  18. func main() {
  19.     var f *os.File
  20.     var err error
  21.     if len(os.Args) < 2 {
  22.         info_log("args err")
  23.         os.Exit(-1)
  24.     }
  25.     f, err = os.Open(os.Args[1])
  26.     if err != nil {
  27.         info_log("open file error")
  28.         os.Exit(-1)
  29.     }

  30.     input := bufio.NewScanner(f)
  31.     var counter = make(map[string]int)
  32.     for input.Scan() {
  33.         counter[input.Text()]++
  34.     }

  35.     for text, n := range counter {
  36.         fmt.Printf("%d: %s\n", n, text)
  37.     }

  38. }
结果输出:
[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]#
---------------------------------------------------------------------------

点击(此处)折叠或打开

  1. package main

  2. import (
  3.     "bufio"
  4.     "fmt"
  5.     "os"
  6. )

  7. func main() {
  8.     counts := make(map[string]int)
  9.     files := os.Args[1:]
  10.     if len(files) == 0 {
  11.         countLines(os.Stdin, counts)
  12.     } else {
  13.         for _, arg := range files {
  14.             f, err := os.Open(arg)
  15.             if err != nil {
  16.                 fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
  17.                 continue
  18.             }
  19.             countLines(f, counts)
  20.             f.Close()
  21.         }
  22.     }
  23.     for line, n := range counts {
  24.         if n > 1 {
  25.             fmt.Printf("%d\t%s\n", n, line)
  26.         }
  27.     }
  28. }

  29. func countLines(f *os.File, counts map[string]int) {
  30.     input := bufio.NewScanner(f)
  31.     for input.Scan() {
  32.         counts[input.Text()]++
  33.     }
  34.     // NOTE: ignoring potential errors from input.Err()
  35. }

  36. //!-
结果输出:
[root@hadoop1 ch1]# ./echo_my aa.txt 
2 hello world
[root@hadoop1 ch1]#
--------------------------------------------------------------------------------

点击(此处)折叠或打开

  1. package main

  2. import (
  3.     "fmt"
  4.     "io/ioutil"
  5.     "os"
  6.     "strings"
  7. )

  8. func main() {
  9.     counts := make(map[string]int)
  10.     for _, filename := range os.Args[1:] {
  11.         data, err := ioutil.ReadFile(filename)
  12.         if err != nil {
  13.             fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
  14.             continue
  15.         }
  16.         for _, line := range strings.Split(string(data), "\n") {
  17.             counts[line]++
  18.         }
  19.     }
  20.     for line, n := range counts {
  21.         if n > 1 {
  22.             fmt.Printf("%d\t%s\n", n, line)
  23.         }
  24.     }
  25. }

  26. //!-
输出结果:
[root@hadoop1 ch1]# ./echo_my aa.txt 
2 hello world
[root@hadoop1 ch1]#

阅读(574) | 评论(0) | 转发(0) |
0

上一篇:echo输出

下一篇:log操作

给主人留下些什么吧!~~