You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 01-chapter3.markdown
+91-88Lines changed: 91 additions & 88 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,53 +3,55 @@
3
3
## grep ##
4
4
5
5
The grep-tool searches for (regular) expressions in text files. Every single line is read and if the line matches the pattern provided on the command line, that line is printed.
6
-
7
-
package main
8
-
9
-
import (
10
-
"flag"
11
-
"regexp"
12
-
"bufio"
13
-
"fmt"
14
-
"os"
15
-
)
16
-
17
-
func grep(re, filename string) {
18
-
regex, err := regexp.Compile(re)
19
-
if err != nil {
20
-
return // there was a problem with the regular expression.
21
-
}
22
-
23
-
fh, err := os.Open(filename)
24
-
f := bufio.NewReader(fh)
25
-
26
-
if err != nil {
27
-
return // there was a problem opening the file.
28
-
}
29
-
defer fh.Close()
30
-
31
-
buf := make([]byte, 1024)
32
-
for {
33
-
buf, _ , err = f.ReadLine()
34
-
if err != nil {
35
-
return
36
-
}
37
-
38
-
s := string(buf)
39
-
if regex.MatchString(s) {
40
-
fmt.Printf("%s\n", string(buf))
41
-
}
42
-
}
43
-
}
44
6
45
-
func main() {
46
-
flag.Parse()
47
-
if flag.NArg() == 2 {
48
-
grep(flag.Arg(0), flag.Arg(1))
49
-
} else {
50
-
fmt.Printf("Wrong number of arguments.\n")
7
+
```go
8
+
package main
9
+
10
+
import (
11
+
"flag"
12
+
"regexp"
13
+
"bufio"
14
+
"fmt"
15
+
"os"
16
+
)
17
+
18
+
funcgrep(re, filenamestring) {
19
+
regex, err:= regexp.Compile(re)
20
+
if err != nil {
21
+
return// there was a problem with the regular expression.
22
+
}
23
+
24
+
fh, err:= os.Open(filename)
25
+
f:= bufio.NewReader(fh)
26
+
27
+
if err != nil {
28
+
return// there was a problem opening the file.
29
+
}
30
+
defer fh.Close()
31
+
32
+
buf:=make([]byte, 1024)
33
+
for {
34
+
buf, _ , err = f.ReadLine()
35
+
if err != nil {
36
+
return
37
+
}
38
+
39
+
s:=string(buf)
40
+
if regex.MatchString(s) {
41
+
fmt.Printf("%s\n", string(buf))
51
42
}
43
+
}
44
+
}
45
+
46
+
funcmain() {
47
+
flag.Parse()
48
+
if flag.NArg() == 2 {
49
+
grep(flag.Arg(0), flag.Arg(1))
50
+
} else {
51
+
fmt.Printf("Wrong number of arguments.\n")
52
52
}
53
+
}
54
+
```
53
55
54
56
If you don't know what grep does, search 'man grep'.
55
57
@@ -60,52 +62,53 @@ This tool is an improved version of grep. It does not only search for a pattern,
60
62
61
63
Usage: ./replacer old new filename
62
64
63
-
64
-
package main
65
-
66
-
import (
67
-
"flag"
68
-
"regexp"
69
-
"bufio"
70
-
"fmt"
71
-
"os"
72
-
)
73
-
74
-
func replace(re, repl, filename string) {
75
-
regex, err := regexp.Compile(re)
76
-
if err != nil {
77
-
return // there was a problem with the regular expression.
78
-
}
79
-
80
-
fh, err := os.Open(filename)
81
-
f := bufio.NewReader(fh)
82
-
83
-
if err != nil {
84
-
return // there was a problem opening the file.
85
-
}
86
-
defer fh.Close()
87
-
88
-
buf := make([]byte, 1024)
89
-
for {
90
-
buf, _ , err = f.ReadLine()
91
-
if err != nil {
92
-
return
93
-
}
94
-
95
-
s := string(buf)
96
-
result := regex.ReplaceAllString(s, repl)
97
-
fmt.Print(result + "\n")
98
-
}
99
-
}
100
-
101
-
func main() {
102
-
flag.Parse()
103
-
if flag.NArg() == 3 {
104
-
repl(flag.Arg(0), flag.Arg(1), flag.Arg(2))
105
-
} else {
106
-
fmt.Printf("Wrong number of arguments.\n")
65
+
```go
66
+
package main
67
+
68
+
import (
69
+
"flag"
70
+
"regexp"
71
+
"bufio"
72
+
"fmt"
73
+
"os"
74
+
)
75
+
76
+
funcreplace(re, repl, filenamestring) {
77
+
regex, err:= regexp.Compile(re)
78
+
if err != nil {
79
+
return// there was a problem with the regular expression.
0 commit comments