\documentclass{beamer}
% \documentclass[handout]{beamer}

\usetheme{dcc}

\usepackage[utf8]{inputenc}
\usepackage[american]{babel}
\usepackage{url}

\newcommand{\ie}{i.\,e.}
\newcommand{\Ie}{I.\,e.}
\newcommand{\eg}{e.\,g.}
\newcommand{\Eg}{E.\,g.}

\title{Mineração de Dados Aplicada}
\subtitle{Selecting Lines}

\author{Loïc Cerf}

\date{April 7th 2025}

\institute
{
  \scriptsize
  DCC -- ICEx -- UFMG
  \bigskip\\
  \includegraphics[width=.334113288\linewidth]{images/dcc}\hfill\includegraphics[width=.5\linewidth]{images/ufmg}
}

\subject{Selecting Lines}

\keywords{data, grep}

\hypersetup{pdfpagemode=FullScreen}

% \AtBeginSection[]
% {
%   \begin{frame}<beamer>
%     \frametitle{Outline}
%     \tableofcontents[currentsection]
%   \end{frame}
% }

\begin{document}

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

\section*{Summary of last session}

\begin{frame}
  \frametitle{Simple text-processing commands (1/2)}
  \begin{itemize}
  \item \texttt{less} interactively reads/searches text;
  \item \texttt{head}/\texttt{tail} outputs the first/last lines;
  \item \texttt{shuf} shuffles lines;
  \item \texttt{cat} concatenates files;
  \item \texttt{tr} replaces, deletes and/or squeezes characters;
  \item \texttt{wc} counts lines, words, and/or characters;
  \item \texttt{cut} selects fields;
  \item \texttt{paste} concatenates lines from different files;
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Simple text-processing commands (2/2)}
  \begin{itemize}
  \item \texttt{sort} sorts lines;
  \item \texttt{comm} compares the lines of two sorted files;
  \item \texttt{join} joins the lines of two sorted files;
  \item \texttt{uniq} reports or omits or counts repeated adjacent
    lines;
  \item \alert{\texttt{grep}} selects the lines matching a regexp.
  \end{itemize}
\end{frame}

\begin{frame}
  \frametitle{Thinking in command flow}
  One should think of these commands as \emph{operators} (like those
  of arithmetic or relational algebra) and consider them when data are
  to be explored or pre-processed.

  \vfill
  \pause

  When applicable, such a flow of specific commands is often the most
  efficient way to achieve the goal, both in terms of \emph{processing
    time} and in terms of \emph{development effort}.
\end{frame}

\section*{Selecting lines}

\begin{frame}
  \frametitle{POSIX Basic Regular Syntax}
  \begin{tabular}{cp{.67\linewidth}}
    Metacharacter & Description\\
    \hline
    \texttt{.} & Any single character\\
    \texttt{[ ]} & Any single character in the brackets\\
    \texttt{[\^{} ]} & Any single character \emph{not} in the
    brackets\\
    \texttt{\^{}} & The starting position of any line\\
    \texttt{\$} & The ending position of any line\\
    \texttt{\textbackslash( \textbackslash)} & The regexp in the
    parentheses as an element\\
    \texttt{\textbackslash N} & The match in the $N^{\text{th}}$ pair
    of parentheses\\
    \texttt{*} & The preceding element zero or more times\\
    \texttt{\textbackslash\{m,n\textbackslash\}} & The preceding
    element at least $m$ and not more than $n$ times
  \end{tabular}

  \vfill
  \pause

  \texttt{\textbackslash} before a metacharacter preserves its literal
  meaning: \texttt{\textbackslash .},
  \texttt{\textbackslash\textbackslash}, etc.
\end{frame}

\begin{frame}
  \frametitle{Selecting according to a regexp}
  The \texttt{grep} command selects the lines matching a regexp.

  \vfill

  A few options: -e REGEXP specifies a regexp, -f FILE for one regexp
  per line in FILE, -i ignores case, -v inverts match, -w matches
  whole words, -x matches whole lines, -c only outputs how many
  matching lines, -l outputs the file names with at least one match,
  -L does the opposite, -m NUM only outputs the NUM first matches, -o
  outputs the \emph{largest} matches per line instead of the whole
  lines, -n shows the line numbers, -A NUM also outputs NUM lines
  after the match, -B NUM before, -C NUM before and after.
\end{frame}

\begin{frame}
  \frametitle{Getting the data}
  \texttt{wget --no-check-certificate -O - \textbackslash{}\\
    \url{dcc.ufmg.br/~lcerf/data.tar.xz} | tar -xJ}
\end{frame}

\begin{frame}
  \frametitle{Exercises}
  \begin{exampleblock}{Exercise}
    Ignoring the case, how many distinct users in
    \texttt{retweets.txt}:
    \begin{itemize}
    \item have ``galo'' but not ``sangalo'' in their names?
    \item do not have any digit in their names?
    \end{itemize}
  \end{exampleblock}

  \vfill
  \pause

  \begin{exampleblock}{Exercise}
    Check that every line of \texttt{retweets.txt} starts with at
    least one space, then an integer, then a space, then an integer,
    then a space, then an integer, then a space, and finally a string
    of alphanumeric characters and the underscore.
  \end{exampleblock}
\end{frame}

\begin{frame}
  \frametitle{Format checking}
  \begin{exampleblock}{With a basic regular expression}
    \texttt{grep -vx}\\
    \texttt{' *\textbackslash ( [0-9][0-9]*\textbackslash
      )\textbackslash \{3\textbackslash \}
      [a-zA-Z0-9\_][a-zA-Z0-9\_]*' retweets.txt}
  \end{exampleblock}

  \vfill
  \pause

  \begin{exampleblock}{With an extended regular expression}
    \texttt{grep -Evx ' *( [0-9]+)\{3\} [a-zA-Z0-9\_]+' retweets.txt}
  \end{exampleblock}
\end{frame}

\begin{frame}
  \frametitle{Exercises}
  \begin{alertblock}{Exercise for next Wednesday}
    In \texttt{retweets.txt}, what users have names that only differ
    by their cases?
  \end{alertblock}

  \vfill
  \pause

  \begin{alertblock}{Exercise for next Monday}
    Given a list of keywords and a text, ignore the letter case and
    use the commands presented so far to:
    \begin{enumerate}
    \item list each keyword occurring in the text;
    \item count the occurrences of each keyword in the text.
    \end{enumerate}
    Both problems can be solved \emph{with} or, sorting all the words
    in the text, \emph{without} grep.  Will you find both ways?
  \end{alertblock}
\end{frame}

\section*{}

\begin{frame}
  \frametitle{License}
  \begin{block}{\copyright 2012--2025 Loïc Cerf}
    \includegraphics[height=1.5ex]{img/licenses/cc-by-sa}\hskip1pt
    These slides are licensed under the Creative Commons
    Attribution-ShareAlike 4.0 International License.
  \end{block}
\end{frame}

\end{document}
