Skip to content

Commit 995d11d

Browse files
authored
Update 01-chapter3.markdown
appended go syntax
1 parent f35a520 commit 995d11d

File tree

1 file changed

+91
-88
lines changed

1 file changed

+91
-88
lines changed

01-chapter3.markdown

Lines changed: 91 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,55 @@
33
## grep ##
44

55
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-
}
446

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+
func grep(re, filename string) {
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))
5142
}
43+
}
44+
}
45+
46+
func main() {
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")
5252
}
53+
}
54+
```
5355
5456
If you don't know what grep does, search 'man grep'.
5557

@@ -60,52 +62,53 @@ This tool is an improved version of grep. It does not only search for a pattern,
6062

6163
Usage: ./replacer old new filename
6264

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+
func replace(re, repl, filename string) {
77+
regex, err := regexp.Compile(re)
78+
if err != nil {
79+
return // there was a problem with the regular expression.
80+
}
81+
82+
fh, err := os.Open(filename)
83+
f := bufio.NewReader(fh)
84+
85+
if err != nil {
86+
return // there was a problem opening the file.
87+
}
88+
defer fh.Close()
89+
90+
buf := make([]byte, 1024)
91+
for {
92+
buf, _ , err = f.ReadLine()
93+
if err != nil {
94+
return
10795
}
96+
97+
s := string(buf)
98+
result := regex.ReplaceAllString(s, repl)
99+
fmt.Print(result + "\n")
100+
}
101+
}
102+
103+
func main() {
104+
flag.Parse()
105+
if flag.NArg() == 3 {
106+
repl(flag.Arg(0), flag.Arg(1), flag.Arg(2))
107+
} else {
108+
fmt.Printf("Wrong number of arguments.\n")
108109
}
110+
}
111+
```
109112
110113
## Verifying an email-address ##
111114

0 commit comments

Comments
 (0)