-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrep.go
63 lines (52 loc) · 1.33 KB
/
grep.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package grep
import (
"bufio"
"fmt"
"os"
"regexp"
"sync"
)
var lines = make(chan string) // 创建一个通道来传递匹配的行
func Grep(pattern string, filename string, concurrentNum int) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
var wg sync.WaitGroup
// 启动多个goroutine来处理每一行
for i := 0; i < concurrentNum; i++ { // 这里可以根据需求调整并发数
wg.Add(1)
go func(threadNum int) {
defer wg.Done()
// TODO: 为什么bufio.NewScanner(file).scanner.Scan()能支持并发扫描文件且不重复?
for scanner.Scan() { // 逐行扫描文件
line := scanner.Text()
//fmt.Printf("Thread %d: %s\n", threadNum, line)
putMatchString2Chan(pattern, line)
//time.Sleep(3 * time.Second)
}
}(i + 1)
}
// 启动一个goroutine来关闭通道并等待所有线程完成
go func() {
wg.Wait()
//fmt.Println("close(lines)")
close(lines)
}()
// 从通道中读取并打印匹配的行
for line := range lines {
fmt.Println(line)
}
if err := scanner.Err(); err != nil {
return err
}
return nil
}
func putMatchString2Chan(pattern string, line string) {
if matched, _ := regexp.MatchString(pattern, line); matched {
//fmt.Println(line)
lines <- line // 将匹配的行发送到通道
}
}