-
Notifications
You must be signed in to change notification settings - Fork 5
/
group3.html
261 lines (200 loc) · 11 KB
/
group3.html
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Regex Tutorial</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="logo">
<h1>ITC134 FL16 LINUX Shell Resource </h1>
</div>
<hr />
<header>
<div id="menu">
<ul>
<li class="inactive"><a href="home.html">Home</a></li>
<li class="inactive"><a href="group1.html">Group 1</a></li>
<li class="inactive"><a href="group2.html">Group 2</a></li>
<li class="current_page_item"><a href="group3.html">Group 3</a></li>
<li class="inactive"><a href="group4.html">Group 4</a></li>
<li class="inactive"><a href="group5.html">Group 5</a></li>
</ul>
</div>
</header>
<div id="page">
<div id="content">
<div class="post">
<h2 class="title"><a href="#"> Regex Tutorial </a></h2>
<div class="entry">
<h2>What is a regular expression?</h2>
<p><pre>
A regular expression, also called a “regex”, is a pattern that describes a set of text strings. This
pattern is used by string searching algorithms for "find" or "find and replace" operations on strings.
Usually a regex contains characters (for literal meaning), anchors (the position in the text, such
as ^, $) and modifier (to expand or narrow the range of text, such as *, \).
</pre><h2>Regex are divided into 3 different types:</h2><pre>
1. Basic regular expressions
2. Interval regular expressions
3. Extended regular expressions
</pre><h2> Here are the commonly used basic regular expressions:</h2><pre>
Symbol Description
. (dot) a single character
? the preceding character matches 0 or 1 times only
* the preceding character matches 0 or more times
+ the preceding character matches 1 or more times
() group several characters to behave as one
| the logical OR operation
^ matches the beginning of the string
$ matches the end of the string
</pre><h2> What are grep and sed command?</h2><pre>
Grep is short for “Global Regular Expression Print”. Grep command is used to search texts or files
that match the given pattern and print the matching result.
</pre>
</p>
</div>
</div>
<div class="post">
<h2 class="title"><a href="#">Regex Tutorial </a></h2>
<div class="entry">
<p><pre>
These are basic regular expressions that do not require any options
* - match zero or more characters
. - match any single character
example - a*t would match any string of characters starting with “a” and ending with
“t”; including at, art, aspect, etc.
- a.t would match any 3 characters starting with “a” and ending with “t”;
including art, ant but not at or aspect
^ - anchor for start of line
$ - anchor for end of line
example - ls -l | grep ^- shows only lines starting with “-” which are the files not
folders
- ^$ would match empty lines
[...] - provide explicit list of matching characters
example - [aeiou] would match any vowel
- [0-9] uses a range specifier to match any numeral
[^...] - provide explicit list of characters not to match
example - [^aeiou] would match any character that is not a vowel
<> - provide whole words to match
example - <sand> matches the whole word "sand" but not “sandbox”
</pre></p>
<pre><code>
#!/bin/bash
# HarvestEmail.sh - general email harvester utility
# requires at least 2 arguments
# $1 is Name of file to which we append harvested emails
# $2-$n are the names of files in which we search for emails
#
StartTime=`date`
# echo initial startup date and time for logging
<span style="color:#47d147">echo</span>"}} HarvestEmail.sh initiated at $StartTime"
# set the initial state of ReturnFlag as 0 or no errors encountered.
ReturnFlag="0"
# create counter for emails
let emailcount=0
<span style="color:#ff4d4d">if</span> [ $# -lt 2 ]; # Do we have less than the required 2 arguments
<span style="color:#ff4d4d">then</span> # not enough arguments so show usage text
# Usage
<span style="color:#47d147">echo</span>"}}"
<span style="color:#47d147">echo</span>"** Error: HarvestEmail.sh requires at least 2 arguments"
<span style="color:#47d147">echo</span>"}}"
<span style="color:#47d147">echo</span>"}} Usage - bash HarvestEmail.sh (target-file) (file-1) ... (file-n)"
<span style="color:#47d147">echo</span>"}} target-file is the folder to which harvested emails will be added"
<span style="color:#47d147">echo</span>"}} file-1 thru file-n are one or more files from which to harvest emails"
<span style="color:#47d147">echo</span>"}}"
ReturnFlag="1"
<span style="color:#ff4d4d">else</span> # we have enough arguments
# create TargetFile variable
TargetFile=$1
# Shift, so we are left with a list of files to search
shift
# echo command line arguments for logging
<span style="color:#47d147">echo</span>"}} Target File to append harvested emails is $TargetFile"
#loop through remaining arguments and echo
<span style="color:#ff4d4d">for</span> searchfile in "$@"
<span style="color:#ff4d4d">do</span>
# confirm this searchfile exists before searching
<span style="color:#ff4d4d">if</span> [ -f $searchfile ]
<span style="color:#ff4d4d">then</span> # searchfile exists
<span style="color:#47d147">echo</span> "}} Searching $searchfile"
# begin harvest emails from searchfile
# create temp copy for processing
<span style="color:#47d147">cp</span> $searchfile workfile1.txt -f
<span style="color:#ff4d4d">if</span> [ $? -eq 0 ]
<span style="color:#ff4d4d">then</span> # successfully copied this searchfile to workfile.txt
# first remove and replace any common mungings to make valid emails
<span style="color:#47d147">sed</span> -i -- 's/\[@\]/@/g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/\.at\./@/gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/ at /@/gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/ (@) /@/gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/(@)/@/gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/ [@] /@/gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/ (at) /@/gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/(at)/@/gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/@/@/g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/\.dot\././gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/ dot /./gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/ (dot) /./gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/(dot)/./gI' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/\.invalid//gI' workfile1.txt
# last remove any common "REMOVEME" mungings
<span style="color:#47d147">sed</span> -i -- 's/\.REMOVETHIS\././g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/\.REMOVETHIS//g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/REMOVETHIS//g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/\.REMOVEME\././g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/\.REMOVEME\././g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/REMOVEME//g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/\.REMOVE\././g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/\.REMOVE//g' workfile1.txt
<span style="color:#47d147">sed</span> -i -- 's/REMOVE/./g' workfile1.txt
# second grep for valid emails
<span style="color:#47d147">grep</span> -i -o '[A-Z0-9._%+-]\+@[A-Z0-9.-]\+\.[A-Z]\{2,4\}' workfile1.txt > workfile2.txt
<span style="color:#47d147">cat</span> workfile2.txt >> $TargetFile
<span style="color:#47d147">cat</span> workfile2.txt | wc -w > countfile.txt
<span style="color:#47d147">let</span> filecount=`cat countfile.txt`
<span style="color:#47d147">let</span> emailcount=$emailcount+$filecount
<span style="color:#47d147">echo</span> "}} $filecount emails harvested"
<span style="color:#ff4d4d">else</span> # error returned when copying searchfile
<span style="color:#47d147">echo</span> "** File IO ERROR searching file $searchfile"
ReturnFlag="1"
fi
<span style="color:#ff4d4d">else</span> # searchfile does not exist
<span style="color:#47d147">echo</span> "** ERROR File $searchfile does not exist"
ReturnFlag="2"
fi
done
fi
# cleanup temp files
<span style="color:#47d147">rm</span> countfile.txt workfile1.txt workfile2.txt
<span style="color:#47d147">echo</span> "}} Total emails harvested was $emailcount"
<span style="color:#47d147">echo</span> "}} HarvestEmail.sh returns errorlevel $ReturnFlag"
<span style="color:#47d147">echo</span> "}} HarvestEmail.sh terminated at `date`"
exit $ReturnFlag
</code></pre>
</div>
</div>
</div>
<div id="sidebar">
<h2> Resources:</h2>
<ul>
<li><a href="http://www.linuxnix.com/regular-expressions-linux-i/">http://www.linuxnix.com/regular-expressions-linux-i/</a></li>
<li><a href="http://ryanstutorials.net/linuxtutorial/grep.php">http://ryanstutorials.net/linuxtutorial/grep.php</a></li>
<li><a href="https://www.techonthenet.com/linux/commands/grep.php">https://www.techonthenet.com/linux/commands/grep.php</a></li>
<li><a href="https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/">https://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/</a></li>
</ul>
</div>
<div style="clear: both;"> </div>
</div>
<footer>
<small>
© 2016, All Rights Reserved
<br />
<a href="http://validator.w3.org/check/referer" target="_blank">Valid HTML</a>
<br />
<a href="http://jigsaw.w3.org/css-validator/check?uri=referer" target="_blank">Valid CSS</a>
</small>
</footer>
</body>
</html>