\documentclass{beamer}

%%%%%%%%%%%%%%%%%%%
% Commands/macros %
%%%%%%%%%%%%%%%%%%%
\newcommand{\link}[1]{
  \begin{flushright}
    \scriptsize{#1}
  \end{flushright}
}

%%%%%%%%%%%%%%%%
% Header/theme %
%%%%%%%%%%%%%%%%
%\usetheme[
%  pageofpages=of,
%  alternativetitlepage=true,
%]{Torino}
%\setbeamertemplate{footline}{}
\setbeamersize{text margin left=20mm} 
\setbeamersize{text margin right=10mm} 

% Import packages
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{times}
\usepackage[T1]{fontenc}
\usepackage{graphics}
\usepackage{hyperref}
\usepackage{ulem}
\usepackage{listings}

%%%%%%%%%%%%%%%%%%%%
% Title page setup %
%%%%%%%%%%%%%%%%%%%%
\subject{%
  sed,
  awk
}

\keywords{%
  sed,
  awk,
  grep,
  unix,
}

\title[Sed and Awk]{%
  Introduction to Sed and Awk scripting
}

\author[Spencer]{%
  Andy Spencer
}

\date[2013-02-02]{%
  February 2, 2013 \\
  \small{San Fernando Valley Linux Users Group}
}

%%%%%%%%%%%%%%%%%
% Content pages %
%%%%%%%%%%%%%%%%%
% Structure:
%   - intro
%     - perl/python
%     - gnu tools only
%   - history
%     - ed
%     - grep
%     - tr
%     - regular expressions
%   - sed
%     - sample command (s/foo/bar/)
%     - command structure
%     - other commands
%     - addresses and ranges
%     - branching
%     - the hold space
%     - fizz buzz
%   - awk
%     - sample command
%       - hello world
%       - sum
%       - average 
%     - command structure
%       - patterns 
%       - actions
%     - record and fields
%     - command line arguments
%     - awk quirks (default values, string concat)
%     - vpaste examples
%       - get_param
%       - cut_file
%       - stat
%       - head
%       - beer
%       - cowlife
%     - programming language
%       - variables, arrays
%       - statements
%       - operators
%       - builtin functions
%         - string manipulation
%         - file/command I/O, networking
%       - function definition
%         - local variables
%       - GNU extensions

\begin{document}

%%%%%%%%%%%%%%%%
% Introduction %
%%%%%%%%%%%%%%%%

% intro
% - perl/python
% - gnu tools only
% history
% - ed
% - grep
% - tr
% - regular expressions

\begin{frame}[plain]
  \titlepage
\end{frame}

\section{Introduction}
\begin{frame}
  \begin{quote}
    One tool for one job?
  \end{quote}
  \begin{quote}
    Those days are dead and gone and the eulogy was delivered by \sout{Perl}
    \textrm{Python}.'' -Rob Pike
  \end{quote}
\end{frame}

\begin{frame}[fragile]{When to use sed and awk?}
  When to use sed?
  \begin{itemize}
    \item When you are ``editing'' a file
    \item Short programs 1-5 lines
  \end{itemize}
  \ \par
  When to use awk?
  \begin{itemize}
    \item When you need to ``summarize'' a file
    \item Slightly longer programs 5-50 lines
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{When not to use them?}
  \begin{itemize}
    \item For longer program, use a programming language
      \begin{itemize}
        \item Awk lacks type checking, etc
      \end{itemize}
    \item For some very simple things
      \begin{itemize}
        \item grep, tr, seq
        \item Unix shell expansion \$\{var/from/to\}
        \item Fancy editor commands (vim, emacs)
      \end{itemize}
  \end{itemize}
\end{frame}

%\begin{frame}[fragile]{History}
%\end{frame}


%%%%%%%%%%%%%%%
% Sed Sed Sed %
%%%%%%%%%%%%%%%

% - sample command (s/foo/bar/)
% - command structure
% - other commands
% - addresses and ranges
% - branching
% - the hold space
% - fizz buzz

\begin{frame}[fragile]{Sed - simple commands}
  Example
\end{frame}

\begin{frame}[fragile]{commands}
  \begin{itemize}
    \item[s] regular expression find/replace
    \item[y] translate characters (like tr)
    \item[p, P] print out the current line
    \item[i, a] insert or append text
    \item[c]    change line
    \item[n, N] read the next line of input
    \item[q, Q] exit/quit
    \item[r, R, w, W] read/write files
    \item[b, t, T] branch to label
    \item[d, D] delete pattern space
    \item[h, H, g, G, x] work with the pattern and hold spaces
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{addresses and ranges}
  \begin{itemize}
    \item[number]     match a line number
    \item[\$]          the last line
    \item[/rege/]     match a regular expression (not the same as s///)
    \item[first~step] match every step'th line starting with first
    \item[0,addr]     match every step'th line starting with first
    \item[addr,+N]    address addr and N following lines
    \item[addr,~N]    match addr and lines up to a multiple of N 
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{branching}
  \begin{itemize}
    \item Can be used for loops
    \item Skipping when a pattern fails
    \item I rarely use these
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{the hold space}
  \begin{itemize}
    \item Can be used for loops
    \item Skipping when a pattern fails
    \item Can be used like a variable
    \item Also rarely used
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{fizz buzz}
  Example
\end{frame}

%%%%%%%%%%%%%%%
% Awk Awk Awk %
%%%%%%%%%%%%%%%

% sample command
% - hello world
% - sum
% - average 
% command structure
% - patterns 
% - actions
% record and fields
% command line arguments
% awk quirks (default values, string concat)
% vpaste examples
% - get_param
% - cut_file
% - stat
% - head
% - beer
% - cowlife
% programming language
% - variables, arrays
% - statements
% - operators
% - builtin functions
%   - string manipulation
%   - file/command I/O, networking
% - function definition
%   - local variables
% - GNU extensions

\begin{frame}[fragile]{Awk - simple commands}
  Example
\end{frame}

\begin{frame}[fragile]{Command structure}
  Basic awk scripts consist of patterns and actions

  When a pattern matches, the action is executed

  Some special patterns: BEGIN, END, BEGINFILE, ENDFILE
  \begin{lstlisting}
    PATTERN { ACTION }
    PATTERN { ACTION }
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Records and fields}
  Text is split twice when reading the file

  Patterns are matched against records

  Fields can be accessed using \$N

  \
  \par
  \begin{itemize}
    \item[Records] by default, a line, changed using RS
    \item[Fields] by default, a word ina line, changed using FS
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Command line arguments}
  \begin{itemize}
    \item[-v] Set a variable (var=val)
    \item[-F] Change field separator, can also be done from BEGIN
  \end{itemize}
  \begin{lstlisting}
    awk -v 'var=$VAR' '{ script }
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Awk quirks}
  \begin{itemize}
    \item default values are 0, no need for declaration
    \item string concatonation is implicit
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{vpaste examples - get param}
  Example
\end{frame}

\begin{frame}[fragile]{vpaste examples - cut file}
  Example
\end{frame}

\begin{frame}[fragile]{vpaste examples - stat}
  Example
\end{frame}

\begin{frame}[fragile]{vpaste examples - head}
  Example
\end{frame}

\begin{frame}[fragile]{vpaste examples - cowlife}
  Example
\end{frame}

\begin{frame}[fragile]{awk as a programming language}
  \begin{itemize}
    \item built-in functions
    \item user defined functions
    \item variables
    \item file i/o
    \item networking
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Variables and arrays}
\end{frame}

\begin{frame}[fragile]{Statements}
  \begin{itemize}
    \item if (condition) body [ else body ]
    \item while (condition) body
    \item for (i=0; i<10; i++) body
    \item for (i in array) body
    \item break, continue
    \item delete var, delete array[index]
    \item swtich (expr) { case value: body; default: body }
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{Builtin functions}
  \begin{itemize}
    \item[Math] atan2, cos, exp, int, log, rand, sin, sqrt, srand
    \item[Strings] asort, gsub, length, match, split, sprintf
    \item[Time] mktime, strftime, systime
    \item[Bitwise] add, or, xor, etc
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{I/O and networking}
  \begin{itemize}
    \item next, nextfile, print, printf
    \item system, fflush
  \end{itemize}
  Reading from files and commands
  \begin{itemize}
    \item getline [var] [<file]
    \item command | getline [var]
    \item command |\& getline [var]
  \end{itemize}
  Writing to files and commands
  \begin{itemize}
    \item print .. > > file
    \item print .. | command
    \item print .. |\& command
  \end{itemize}
\end{frame}

\begin{frame}[fragile]{User defined functions}
  \begin{lstlisting}
    function sayhi(name,        str) {
        str = "Hello, " name "!'
        print str
    }
  \end{lstlisting}
\end{frame}

\begin{frame}[fragile]{GNU extensions}
  Example
\end{frame}

\end{document}
