]> Pileus Git - ~andy/sfvlug/blob - awk/sedawk.tex
Add sed and awk presentation
[~andy/sfvlug] / awk / sedawk.tex
1 \documentclass{beamer}
2
3 %%%%%%%%%%%%%%%%%%%
4 % Commands/macros %
5 %%%%%%%%%%%%%%%%%%%
6 \newcommand{\link}[1]{
7   \begin{flushright}
8     \scriptsize{#1}
9   \end{flushright}
10 }
11
12 %%%%%%%%%%%%%%%%
13 % Header/theme %
14 %%%%%%%%%%%%%%%%
15 %\usetheme[
16 %  pageofpages=of,
17 %  alternativetitlepage=true,
18 %]{Torino}
19 %\setbeamertemplate{footline}{}
20 \setbeamersize{text margin left=20mm} 
21 \setbeamersize{text margin right=10mm} 
22
23 % Import packages
24 \usepackage[english]{babel}
25 \usepackage[latin1]{inputenc}
26 \usepackage{times}
27 \usepackage[T1]{fontenc}
28 \usepackage{graphics}
29 \usepackage{hyperref}
30 \usepackage{ulem}
31 \usepackage{listings}
32
33 %%%%%%%%%%%%%%%%%%%%
34 % Title page setup %
35 %%%%%%%%%%%%%%%%%%%%
36 \subject{%
37   sed,
38   awk
39 }
40
41 \keywords{%
42   sed,
43   awk,
44   grep,
45   unix,
46 }
47
48 \title[Sed and Awk]{%
49   Introduction to Sed and Awk scripting
50 }
51
52 \author[Spencer]{%
53   Andy Spencer
54 }
55
56 \date[2013-02-02]{%
57   February 2, 2013 \\
58   \small{San Fernando Valley Linux Users Group}
59 }
60
61 %%%%%%%%%%%%%%%%%
62 % Content pages %
63 %%%%%%%%%%%%%%%%%
64 % Structure:
65 %   - intro
66 %     - perl/python
67 %     - gnu tools only
68 %   - history
69 %     - ed
70 %     - grep
71 %     - tr
72 %     - regular expressions
73 %   - sed
74 %     - sample command (s/foo/bar/)
75 %     - command structure
76 %     - other commands
77 %     - addresses and ranges
78 %     - branching
79 %     - the hold space
80 %     - fizz buzz
81 %   - awk
82 %     - sample command
83 %       - hello world
84 %       - sum
85 %       - average 
86 %     - command structure
87 %       - patterns 
88 %       - actions
89 %     - record and fields
90 %     - command line arguments
91 %     - awk quirks (default values, string concat)
92 %     - vpaste examples
93 %       - get_param
94 %       - cut_file
95 %       - stat
96 %       - head
97 %       - beer
98 %       - cowlife
99 %     - programming language
100 %       - variables, arrays
101 %       - statements
102 %       - operators
103 %       - builtin functions
104 %         - string manipulation
105 %         - file/command I/O, networking
106 %       - function definition
107 %         - local variables
108 %       - GNU extensions
109
110 \begin{document}
111
112 %%%%%%%%%%%%%%%%
113 % Introduction %
114 %%%%%%%%%%%%%%%%
115
116 % intro
117 % - perl/python
118 % - gnu tools only
119 % history
120 % - ed
121 % - grep
122 % - tr
123 % - regular expressions
124
125 \begin{frame}[plain]
126   \titlepage
127 \end{frame}
128
129 \section{Introduction}
130 \begin{frame}
131   \begin{quote}
132     One tool for one job?
133   \end{quote}
134   \begin{quote}
135     Those days are dead and gone and the eulogy was delivered by \sout{Perl}
136     \textrm{Python}.'' -Rob Pike
137   \end{quote}
138 \end{frame}
139
140 \begin{frame}[fragile]{When to use sed and awk?}
141   When to use sed?
142   \begin{itemize}
143     \item When you are ``editing'' a file
144     \item Short programs 1-5 lines
145   \end{itemize}
146   \ \par
147   When to use awk?
148   \begin{itemize}
149     \item When you need to ``summarize'' a file
150     \item Slightly longer programs 5-50 lines
151   \end{itemize}
152 \end{frame}
153
154 \begin{frame}[fragile]{When not to use them?}
155   \begin{itemize}
156     \item For longer program, use a programming language
157       \begin{itemize}
158         \item Awk lacks type checking, etc
159       \end{itemize}
160     \item For some very simple things
161       \begin{itemize}
162         \item grep, tr, seq
163         \item Unix shell expansion \$\{var/from/to\}
164         \item Fancy editor commands (vim, emacs)
165       \end{itemize}
166   \end{itemize}
167 \end{frame}
168
169 %\begin{frame}[fragile]{History}
170 %\end{frame}
171
172
173 %%%%%%%%%%%%%%%
174 % Sed Sed Sed %
175 %%%%%%%%%%%%%%%
176
177 % - sample command (s/foo/bar/)
178 % - command structure
179 % - other commands
180 % - addresses and ranges
181 % - branching
182 % - the hold space
183 % - fizz buzz
184
185 \begin{frame}[fragile]{Sed - simple commands}
186   Example
187 \end{frame}
188
189 \begin{frame}[fragile]{commands}
190   \begin{itemize}
191     \item[s] regular expression find/replace
192     \item[y] translate characters (like tr)
193     \item[p, P] print out the current line
194     \item[i, a] insert or append text
195     \item[c]    change line
196     \item[n, N] read the next line of input
197     \item[q, Q] exit/quit
198     \item[r, R, w, W] read/write files
199     \item[b, t, T] branch to label
200     \item[d, D] delete pattern space
201     \item[h, H, g, G, x] work with the pattern and hold spaces
202   \end{itemize}
203 \end{frame}
204
205 \begin{frame}[fragile]{addresses and ranges}
206   \begin{itemize}
207     \item[number]     match a line number
208     \item[\$]          the last line
209     \item[/rege/]     match a regular expression (not the same as s///)
210     \item[first~step] match every step'th line starting with first
211     \item[0,addr]     match every step'th line starting with first
212     \item[addr,+N]    address addr and N following lines
213     \item[addr,~N]    match addr and lines up to a multiple of N 
214   \end{itemize}
215 \end{frame}
216
217 \begin{frame}[fragile]{branching}
218   \begin{itemize}
219     \item Can be used for loops
220     \item Skipping when a pattern fails
221     \item I rarely use these
222   \end{itemize}
223 \end{frame}
224
225 \begin{frame}[fragile]{the hold space}
226   \begin{itemize}
227     \item Can be used for loops
228     \item Skipping when a pattern fails
229     \item Can be used like a variable
230     \item Also rarely used
231   \end{itemize}
232 \end{frame}
233
234 \begin{frame}[fragile]{fizz buzz}
235   Example
236 \end{frame}
237
238 %%%%%%%%%%%%%%%
239 % Awk Awk Awk %
240 %%%%%%%%%%%%%%%
241
242 % sample command
243 % - hello world
244 % - sum
245 % - average 
246 % command structure
247 % - patterns 
248 % - actions
249 % record and fields
250 % command line arguments
251 % awk quirks (default values, string concat)
252 % vpaste examples
253 % - get_param
254 % - cut_file
255 % - stat
256 % - head
257 % - beer
258 % - cowlife
259 % programming language
260 % - variables, arrays
261 % - statements
262 % - operators
263 % - builtin functions
264 %   - string manipulation
265 %   - file/command I/O, networking
266 % - function definition
267 %   - local variables
268 % - GNU extensions
269
270 \begin{frame}[fragile]{Awk - simple commands}
271   Example
272 \end{frame}
273
274 \begin{frame}[fragile]{Command structure}
275   Basic awk scripts consist of patterns and actions
276
277   When a pattern matches, the action is executed
278
279   Some special patterns: BEGIN, END, BEGINFILE, ENDFILE
280   \begin{lstlisting}
281     PATTERN { ACTION }
282     PATTERN { ACTION }
283   \end{lstlisting}
284 \end{frame}
285
286 \begin{frame}[fragile]{Records and fields}
287   Text is split twice when reading the file
288
289   Patterns are matched against records
290
291   Fields can be accessed using \$N
292
293   \
294   \par
295   \begin{itemize}
296     \item[Records] by default, a line, changed using RS
297     \item[Fields] by default, a word ina line, changed using FS
298   \end{itemize}
299 \end{frame}
300
301 \begin{frame}[fragile]{Command line arguments}
302   \begin{itemize}
303     \item[-v] Set a variable (var=val)
304     \item[-F] Change field separator, can also be done from BEGIN
305   \end{itemize}
306   \begin{lstlisting}
307     awk -v 'var=$VAR' '{ script }
308   \end{lstlisting}
309 \end{frame}
310
311 \begin{frame}[fragile]{Awk quirks}
312   \begin{itemize}
313     \item default values are 0, no need for declaration
314     \item string concatonation is implicit
315   \end{itemize}
316 \end{frame}
317
318 \begin{frame}[fragile]{vpaste examples - get param}
319   Example
320 \end{frame}
321
322 \begin{frame}[fragile]{vpaste examples - cut file}
323   Example
324 \end{frame}
325
326 \begin{frame}[fragile]{vpaste examples - stat}
327   Example
328 \end{frame}
329
330 \begin{frame}[fragile]{vpaste examples - head}
331   Example
332 \end{frame}
333
334 \begin{frame}[fragile]{vpaste examples - cowlife}
335   Example
336 \end{frame}
337
338 \begin{frame}[fragile]{awk as a programming language}
339   \begin{itemize}
340     \item built-in functions
341     \item user defined functions
342     \item variables
343     \item file i/o
344     \item networking
345   \end{itemize}
346 \end{frame}
347
348 \begin{frame}[fragile]{Variables and arrays}
349 \end{frame}
350
351 \begin{frame}[fragile]{Statements}
352   \begin{itemize}
353     \item if (condition) body [ else body ]
354     \item while (condition) body
355     \item for (i=0; i<10; i++) body
356     \item for (i in array) body
357     \item break, continue
358     \item delete var, delete array[index]
359     \item swtich (expr) { case value: body; default: body }
360   \end{itemize}
361 \end{frame}
362
363 \begin{frame}[fragile]{Builtin functions}
364   \begin{itemize}
365     \item[Math] atan2, cos, exp, int, log, rand, sin, sqrt, srand
366     \item[Strings] asort, gsub, length, match, split, sprintf
367     \item[Time] mktime, strftime, systime
368     \item[Bitwise] add, or, xor, etc
369   \end{itemize}
370 \end{frame}
371
372 \begin{frame}[fragile]{I/O and networking}
373   \begin{itemize}
374     \item next, nextfile, print, printf
375     \item system, fflush
376   \end{itemize}
377   Reading from files and commands
378   \begin{itemize}
379     \item getline [var] [<file]
380     \item command | getline [var]
381     \item command |\& getline [var]
382   \end{itemize}
383   Writing to files and commands
384   \begin{itemize}
385     \item print .. > > file
386     \item print .. | command
387     \item print .. |\& command
388   \end{itemize}
389 \end{frame}
390
391 \begin{frame}[fragile]{User defined functions}
392   \begin{lstlisting}
393     function sayhi(name,        str) {
394         str = "Hello, " name "!'
395         print str
396     }
397   \end{lstlisting}
398 \end{frame}
399
400 \begin{frame}[fragile]{GNU extensions}
401   Example
402 \end{frame}
403
404 \end{document}