17 % alternativetitlepage=true,
19 %\setbeamertemplate{footline}{}
20 \setbeamersize{text margin left=20mm}
21 \setbeamersize{text margin right=10mm}
24 \usepackage[english]{babel}
25 \usepackage[latin1]{inputenc}
27 \usepackage[T1]{fontenc}
49 Introduction to Sed and Awk scripting
58 \small{San Fernando Valley Linux Users Group}
72 % - regular expressions
74 % - sample command (s/foo/bar/)
77 % - addresses and ranges
90 % - command line arguments
91 % - awk quirks (default values, string concat)
99 % - programming language
100 % - variables, arrays
103 % - builtin functions
104 % - string manipulation
105 % - file/command I/O, networking
106 % - function definition
123 % - regular expressions
129 \section{Introduction}
132 One tool for one job?
135 Those days are dead and gone and the eulogy was delivered by \sout{Perl}
136 \textrm{Python}.'' -Rob Pike
140 \begin{frame}[fragile]{When to use sed and awk?}
143 \item When you are ``editing'' a file
144 \item Short programs 1-5 lines
149 \item When you need to ``summarize'' a file
150 \item Slightly longer programs 5-50 lines
154 \begin{frame}[fragile]{When not to use them?}
156 \item For longer program, use a programming language
158 \item Awk lacks type checking, etc
160 \item For some very simple things
163 \item Unix shell expansion \$\{var/from/to\}
164 \item Fancy editor commands (vim, emacs)
169 %\begin{frame}[fragile]{History}
177 % - sample command (s/foo/bar/)
178 % - command structure
180 % - addresses and ranges
185 \begin{frame}[fragile]{Sed - simple commands}
189 \begin{frame}[fragile]{commands}
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
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
205 \begin{frame}[fragile]{addresses and ranges}
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
217 \begin{frame}[fragile]{branching}
219 \item Can be used for loops
220 \item Skipping when a pattern fails
221 \item I rarely use these
225 \begin{frame}[fragile]{the hold space}
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
234 \begin{frame}[fragile]{fizz buzz}
250 % command line arguments
251 % awk quirks (default values, string concat)
259 % programming language
260 % - variables, arrays
263 % - builtin functions
264 % - string manipulation
265 % - file/command I/O, networking
266 % - function definition
270 \begin{frame}[fragile]{Awk - simple commands}
274 \begin{frame}[fragile]{Command structure}
275 Basic awk scripts consist of patterns and actions
277 When a pattern matches, the action is executed
279 Some special patterns: BEGIN, END, BEGINFILE, ENDFILE
286 \begin{frame}[fragile]{Records and fields}
287 Text is split twice when reading the file
289 Patterns are matched against records
291 Fields can be accessed using \$N
296 \item[Records] by default, a line, changed using RS
297 \item[Fields] by default, a word ina line, changed using FS
301 \begin{frame}[fragile]{Command line arguments}
303 \item[-v] Set a variable (var=val)
304 \item[-F] Change field separator, can also be done from BEGIN
307 awk -v 'var=$VAR' '{ script }
311 \begin{frame}[fragile]{Awk quirks}
313 \item default values are 0, no need for declaration
314 \item string concatonation is implicit
318 \begin{frame}[fragile]{vpaste examples - get param}
322 \begin{frame}[fragile]{vpaste examples - cut file}
326 \begin{frame}[fragile]{vpaste examples - stat}
330 \begin{frame}[fragile]{vpaste examples - head}
334 \begin{frame}[fragile]{vpaste examples - cowlife}
338 \begin{frame}[fragile]{awk as a programming language}
340 \item built-in functions
341 \item user defined functions
348 \begin{frame}[fragile]{Variables and arrays}
351 \begin{frame}[fragile]{Statements}
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 }
363 \begin{frame}[fragile]{Builtin functions}
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
372 \begin{frame}[fragile]{I/O and networking}
374 \item next, nextfile, print, printf
377 Reading from files and commands
379 \item getline [var] [<file]
380 \item command | getline [var]
381 \item command |\& getline [var]
383 Writing to files and commands
385 \item print .. > > file
386 \item print .. | command
387 \item print .. |\& command
391 \begin{frame}[fragile]{User defined functions}
393 function sayhi(name, str) {
394 str = "Hello, " name "!'
400 \begin{frame}[fragile]{GNU extensions}