\documentclass[ignorenonframetext,]{beamer}
\setbeamertemplate{caption}[numbered]
\setbeamertemplate{caption label separator}{: }
\setbeamercolor{caption name}{fg=normal text.fg}
\beamertemplatenavigationsymbolsempty
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\else % if luatex or xelatex
  \ifxetex
    \usepackage{mathspec}
  \else
    \usepackage{fontspec}
  \fi
  \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\newif\ifbibliography
\hypersetup{
            pdftitle={Expressões regulares},
            pdfborder={0 0 0},
            breaklinks=true}
\urlstyle{same}  % don't use monospace font for urls
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}

% Prevent slide breaks in the middle of a paragraph:
\widowpenalties 1 10000
\raggedbottom

\AtBeginPart{
  \let\insertpartnumber\relax
  \let\partname\relax
  \frame{\partpage}
}
\AtBeginSection{
  \ifbibliography
  \else
    \let\insertsectionnumber\relax
    \let\sectionname\relax
    \frame{\sectionpage}
  \fi
}
\AtBeginSubsection{
  \let\insertsubsectionnumber\relax
  \let\subsectionname\relax
  \frame{\subsectionpage}
}

\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}
%-----------------------------------------------------------------------

\usepackage[T1]{fontenc}

\usepackage[math]{iwona}
% \usepackage[math]{kurier}

% \usepackage[sfdefault]{roboto}
\usepackage[sfdefault, condensed]{roboto}
% \usepackage[default]{sourcesanspro}

% \usefonttheme[onlymath]{serif}
% \usefonttheme[onlymath]{sans}

% \usepackage{csquotes}

% % Font for code. ----------------------------
% \usepackage{inconsolata}
\usepackage[scaled=0.8]{beramono}

%-----------------------------------------------------------------------

% \usepackage{lmodern}
\usepackage{amssymb, amsmath}
\setcounter{MaxMatrixCols}{20} % bmatrix, max number of columns.

% \usepackage{ifxetex, ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\usepackage[utf8]{inputenc}
\usepackage[shorthands=off,main=brazil]{babel}
\usepackage{graphicx}

\usepackage{xcolor}
\usepackage{setspace}
\usepackage{xspace}
\usepackage{tabularx}

\setlength{\belowcaptionskip}{-0.5em}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em}  % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{0}

%-----------------------------------------------------------------------
% Definições de esquema de cores.

% Ubuntu.
% http://www.color-hex.com/color-palette/2018
\definecolor{ubuntu_purple}{HTML}{5E2750} % Título.
\definecolor{ubuntu_orange}{HTML}{DD4814} % Estrutura.
\definecolor{gray_dark}{HTML}{333333}  % Texto.
\definecolor{gray_light}{HTML}{DDDDDD} % Faixa.

%-----------------------------------------------------------------------
% Bibliography.

% \usepackage[style=authoryear]{biblatex}
% Use:
%   \cite{<ref>}
%   \parencite{<ref>}
%   \fullcite{<ref>}
%   \footfullcite{<ref>}
% ATTENTION: Compilation = pdflatex > biber > pdflatex > pdflatex.
% Call refs.bib at preamble with: \addbibresource{refs.bib}
%
%-----------------------------------------------------------------------

\usepackage{caption}
\captionsetup{
  font=footnotesize,
  labelfont={color=ubuntu_orange},
  labelsep=period}
\usepackage{subfig}

\makeatletter
\let\@@magyar@captionfix\relax
\makeatother

\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}

%-----------------------------------------------------------------------

\usepackage{tikz}

\usebackgroundtemplate{
  \tikz[overlay, remember picture]
  \node[at = (current page.center),
        inner sep = 0pt] {%
          % \includegraphics[keepaspectratio, height=\paperheight]{config/ufpr-imprensa-blur-4x3.jpg}
          % \includegraphics[keepaspectratio, height=\paperheight]{/home/walmes/Projects/templates/COMMON/ufpr-perto-1.jpg}
          % \includegraphics[keepaspectratio, height=\paperheight]{/home/walmes/Projects/templates/COMMON/ufpr-fachada-esq-1.jpg}
          \includegraphics[keepaspectratio, height=\paperheight]{/home/walmes/Projects/templates/COMMON/ufpr-fachada-baixo-1.jpg}
          % \includegraphics[keepaspectratio, height=\paperheight]{/home/walmes/Projects/templates/COMMON/ufpr-fundo-16x9.jpg}
        };
}

%-----------------------------------------------------------------------

\hypersetup{
  colorlinks=true,
  linkcolor=ubuntu_orange,
  urlcolor=ubuntu_purple,
  citecolor=ubuntu_purple
}

%-----------------------------------------------------------------------
% ATTENTION: http://www.cpt.univ-mrs.fr/~masson/latex/Beamer-appearance-cheat-sheet.pdf

\setbeamerfont{title}{series=\bfseries, size=\huge, parent=structure}
\setbeamerfont{subtitle}{series=\bfseries, size=\Large, parent=structure}
\setbeamerfont{frametitle}{series=\bfseries, size=\LARGE, parent=structure}

\setbeamertemplate{caption}[numbered]
\setbeamertemplate{caption label separator}{: }
\setbeamertemplate{caption}[numbered]{}
\setbeamertemplate{section in toc}[sections numbered]
\setbeamertemplate{subsection in toc}[subsections numbered]
\setbeamertemplate{sections/subsections in toc}[ball]{}
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}{}
\setbeamertemplate{frametitle continuation}{\gdef\beamer@frametitle{}\vspace*{1ex}}

\setbeamertemplate{enumerate items}[default]
\setbeamertemplate{itemize items}{\scriptsize\raise1.25pt\hbox{\donotcoloroutermaths$\blacktriangleright$}}

% Rodapé.
\setbeamercolor{title in head/foot}{parent=subsection in head/foot}
\setbeamercolor{author in head/foot}{bg=ubuntu_orange, fg=white}
\setbeamercolor{date in head/foot}{parent=subsection in head/foot, fg=ubuntu_orange}

% Cabeçalho.
\setbeamercolor{section in head/foot}{bg=ubuntu_purple, fg=ubuntu_orange}
\setbeamercolor{subsection in head/foot}{bg=ubuntu_purple, fg=white}

\setbeamercolor{title}{fg=ubuntu_purple}       % Título dos slides.
\setbeamercolor{subtitle}{fg=ubuntu_orange}    % Subtítulo.
\setbeamercolor{institute}{fg=gray_dark}       % Instituição.
\setbeamercolor{frametitle}{fg=ubuntu_purple}  % De quadro.
\setbeamercolor{structure}{fg=ubuntu_orange}   % Listas e rodapé.
\setbeamercolor{item projected}{bg=gray_dark}
\setbeamercolor{normal text}{fg=gray_dark}     % Texto.

%-----------------------------------------------------------------------
% To remove empty brackets of \institution.

\makeatletter
\setbeamertemplate{footline}{
  \leavevmode%
  \hbox{%
    \begin{beamercolorbox}[
      wd=0.3\paperwidth, ht=2.25ex, dp=1ex, right]{author in head/foot}%
      \usebeamerfont{author in head/foot}\insertshortauthor{}\hspace*{1ex}
    \end{beamercolorbox}%
    \begin{beamercolorbox}[
      wd=0.6\paperwidth, ht=2.25ex, dp=1ex, left]{title in head/foot}%
      \usebeamerfont{title in head/foot}\hspace*{1ex}\bfseries\insertshorttitle{}
    \end{beamercolorbox}%
    \begin{beamercolorbox}[
      wd=0.1\paperwidth, ht=2.25ex, dp=1ex, right]{date in head/foot}%
      \bfseries\insertframenumber{}\hspace*{2ex}
    \end{beamercolorbox}
  }%
  \vskip0pt%
}
\makeatother

%-----------------------------------------------------------------------
%---- preamble-author.tex ----------------------------------------------

\author[Walmes Zeviani $\cdot$ UFPR]{%
      \href{http://leg.ufpr.br/~walmes}{Prof.~Walmes Zeviani} \\
      \href{mailto:walmes@ufpr.br}{\small\tt walmes@ufpr.br}
}

\institute[DEST/UFPR]{
  {Laboratório de Estatística e Geoinformação}\\
  {Departamento de Estatística}\\
  {Universidade Federal do Paraná}}

%-----------------------------------------------------------------------
%---- preamble-chunk.tex -----------------------------------------------

% Knitr.

% ATTENTION: this needs `\usepackage{xcolor}'.
\definecolor{color_line}{HTML}{333333}
\definecolor{color_back}{HTML}{DDDDDD}

% R input.
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{
  frame=lines,
  framesep=1ex,
  framerule=0.5pt,
  rulecolor=\color{color_line},
  numbers=right,
  fontsize=\footnotesize,
  baselinestretch=0.75,
  commandchars=\\\{\}}

% Margens do ambiente `Shaded'.
% \fvset{listparameters={\setlength{\topsep}{-1em}}}
\renewenvironment{Shaded}{\vspace{-1ex}}{\vspace{-2ex}}

% R output e todo `verbatim'.
\makeatletter
\def\verbatim@font{\linespread{0.75}\normalfont\ttfamily\footnotesize}
\makeatother

% Cor de fundo e margens do `vertabim'.
\let\oldv\verbatim
\let\oldendv\endverbatim

\def\verbatim{%
  \par\setbox0\vbox\bgroup
  \vspace{-1.5\topsep}
  \oldv
}
\def\endverbatim{%
  \oldendv
  \vspace{-0.9\topsep}
  \egroup\fboxsep0pt
  \noindent{\colorbox{color_back}{\usebox0}}\par
}

%-----------------------------------------------------------------------

\title{Expressões regulares}
\date{}

\begin{document}
\frame{\titlepage}

\begin{frame}[fragile]{Objetivo e justificativa}

\begin{itemize}
\tightlist
\item
  Análise de texto sempre requer \textbf{processamento de caracteres}.
\item
  Definir o que é expressão regular.
\item
  Listar material e recursos para aprendizado.
\item
  Apresentar os recursos do R.
\item
  Introduzir o \texttt{sed} e \texttt{awk}.
\end{itemize}

\end{frame}

\section{Expressões regulares}\label{expressoes-regulares}

\begin{frame}{Detalhes}

\begin{itemize}
\tightlist
\item
  Regular Expression = \emph{regex} ou \emph{regexp}.
\item
  Sequencia concisa de (meta)caracteres que definem um padrão.
\item
  1950s - Stephen Cole Kleene (matemático).
\item
  O conceito passou a ser usado nos editores de texto do Unix.
\item
  Usado em:

  \begin{itemize}
  \tightlist
  \item
    Motores de busca.
  \item
    Editores/processadores de texto.
  \item
    Análise léxica.
  \item
    Para \emph{procurar} ou \emph{procurar e substituir}.
  \item
    Extrair informações chave de documentos de texto (TEL, CEP, CPF,
    email, datas, horários, cifras monetárias).
  \end{itemize}
\item
  Sintaxes:

  \begin{itemize}
  \tightlist
  \item
    Padrão POSIX.
  \item
    Padrão Perl.
  \end{itemize}
\end{itemize}

\end{frame}

\begin{frame}{Exemplo}

\begin{figure}

{\centering \includegraphics[width=1\linewidth]{img/email-regular-expression} 

}

\caption{Expressão regular para bater com emails. Fonte: \url{http://learnwebtutorials.com/why-regular-expression-is-so-confusing}.}\label{fig:unnamed-chunk-2}
\end{figure}

\end{frame}

\begin{frame}{Folhas de cola}

As folhas de cola são um guia de consulta rápido para fazer a contrução
de expressões regulares.

\begin{itemize}
\tightlist
\item
  \url{http://mesguerra.org/tricks/regexp/regexp.pdf}.
\item
  \url{https://www.rstudio.com/wp-content/uploads/2016/09/RegExCheatsheet.pdf}.
\item
  \url{http://www.rexegg.com/regex-quickstart.html}.
\end{itemize}

\end{frame}

\begin{frame}{Testadores online}

Os testadores \emph{online} servem para avaliar a expressão regular
conhecida. Alguns editores possuem recursos para desenvolvimento e
aplicação de regex, como o Emacs:
\url{https://www.masteringemacs.org/article/re-builder-interactive-regexp-builder}.

\begin{itemize}
\tightlist
\item
  \url{http://regexr.com/}.
\item
  \url{http://www.regextester.com/}.
\item
  \url{https://regex101.com/}.
\item
  \url{http://www.freeformatter.com/regex-tester.html}.
\end{itemize}

\end{frame}

\begin{frame}{Praticar}

\begin{itemize}
\tightlist
\item
  Data: dd/mm/yyyy.
\item
  CEP: 00.000-000.
\item
  Valor monetário: R\$ 000,00.
\end{itemize}

\end{frame}

\section{Regex no R}\label{regex-no-r}

\begin{frame}[fragile]{Recursos básicos}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{help}\NormalTok{(regex, }\DataTypeTok{help_type =} \StringTok{"html"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{itemize}
\tightlist
\item
  \texttt{grep()} e \texttt{grepl()}: Detecta o padrão.
\item
  \texttt{sub()} e \texttt{gsub()}: Substitui o padrão.
\item
  \texttt{regexpr()} e \texttt{gregexpr()}: Localiza o padrão.
\item
  \texttt{strsplit()}: Divide no padrão.
\end{itemize}

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\tightlist
\item
  Dentro de \emph{strings}, deve-se usar duplo contra barra para tornar
  metacaracteres literais.
\item
  O comportamento default é POSIX. Faça \texttt{perl\ =\ TRUE} para usar
  o Perl.
\end{enumerate}

\end{frame}

\begin{frame}[fragile]{Exemplo de detecção}

\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{s <-}\StringTok{ }\KeywordTok{colors}\NormalTok{()}

\CommentTok{# Cores que tenham "red".}
\KeywordTok{grep}\NormalTok{(}\DataTypeTok{pattern =} \StringTok{"red"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ s)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  [1] 100 372 373 374 375 376 476 503 504 505 506 507 524 525 526 527
## [17] 528 552 553 554 555 556 641 642 643 644 645
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{grep}\NormalTok{(}\DataTypeTok{pattern =} \StringTok{"red"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ s, }\DataTypeTok{value =} \OtherTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  [1] "darkred"         "indianred"       "indianred1"     
##  [4] "indianred2"      "indianred3"      "indianred4"     
##  [7] "mediumvioletred" "orangered"       "orangered1"     
## [10] "orangered2"      "orangered3"      "orangered4"     
## [13] "palevioletred"   "palevioletred1"  "palevioletred2" 
## [16] "palevioletred3"  "palevioletred4"  "red"            
## [19] "red1"            "red2"            "red3"           
## [22] "red4"            "violetred"       "violetred1"     
## [25] "violetred2"      "violetred3"      "violetred4"
\end{verbatim}

\end{frame}

\begin{frame}[fragile]{Exemplo de substituição}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Que tenham "red" seguido de um número.}
\KeywordTok{grep}\NormalTok{(}\DataTypeTok{pattern =} \StringTok{"red}\CharTok{\textbackslash{}\textbackslash{}}\StringTok{d"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ s, }\DataTypeTok{value =} \OtherTok{TRUE}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  [1] "indianred1"     "indianred2"     "indianred3"    
##  [4] "indianred4"     "orangered1"     "orangered2"    
##  [7] "orangered3"     "orangered4"     "palevioletred1"
## [10] "palevioletred2" "palevioletred3" "palevioletred4"
## [13] "red1"           "red2"           "red3"          
## [16] "red4"           "violetred1"     "violetred2"    
## [19] "violetred3"     "violetred4"
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# grep(pattern = "red[0-9]", x = s, value = TRUE)}

\CommentTok{# Extrair o que está a esqueda de "red".}
\NormalTok{red <-}\StringTok{ }\KeywordTok{grep}\NormalTok{(}\DataTypeTok{pattern =} \StringTok{"red}\CharTok{\textbackslash{}\textbackslash{}}\StringTok{d"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ s, }\DataTypeTok{value =} \OtherTok{TRUE}\NormalTok{)}
\KeywordTok{sub}\NormalTok{(}\DataTypeTok{pattern =} \StringTok{"^(.*)red.*"}\NormalTok{, }\DataTypeTok{replacement =} \StringTok{"}\CharTok{\textbackslash{}\textbackslash{}}\StringTok{1"}\NormalTok{, }\DataTypeTok{x =}\NormalTok{ red)}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
##  [1] "indian"     "indian"     "indian"     "indian"    
##  [5] "orange"     "orange"     "orange"     "orange"    
##  [9] "paleviolet" "paleviolet" "paleviolet" "paleviolet"
## [13] ""           ""           ""           ""          
## [17] "violet"     "violet"     "violet"     "violet"
\end{verbatim}

\end{frame}

\begin{frame}[fragile]{Pacote \texttt{stringr} e \texttt{stringi}}

\begin{itemize}
\tightlist
\item
  \texttt{stringi}: Character String Processing Facilities,
  \url{https://cran.r-project.org/package=stringi}.
\item
  \texttt{stringr}: Simple, Consistent Wrappers for Common String
  Operations, \url{https://cran.r-project.org/package=stringr}.
\item
  Consulte as \emph{vignettes} do \texttt{stringr} para uma rápida
  introdução aos recursos.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(stringr)}
\KeywordTok{ls}\NormalTok{(}\StringTok{"package:stringr"}\NormalTok{)}

\KeywordTok{library}\NormalTok{(stringi)}
\KeywordTok{ls}\NormalTok{(}\StringTok{"package:stringi"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\end{frame}

\section{Recursos do Linux}\label{recursos-do-linux}

\begin{frame}[fragile]{\texttt{grep}}

\begin{itemize}
\tightlist
\item
  Buscador de \emph{regex} em arquivos.
\item
  Serve para mostrar quais arquivos contém determinada \emph{regex}.
\item
  \url{https://www.freebsd.org/cgi/man.cgi?query=grep}.
\end{itemize}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Exibe a lista de arquivos que contém ocorrência de "title:".}
\FunctionTok{grep}\NormalTok{ -r }\StringTok{'title:'}\NormalTok{ *}

\CommentTok{# # Restringe para arquivos de extensão Rmd e numera as linhas.}
\FunctionTok{grep}\NormalTok{ -n -r --include=*.Rmd }\StringTok{'title:'}\NormalTok{ *}

\CommentTok{# Esconde (hide) o nome do arquivo.}
\FunctionTok{grep}\NormalTok{ -h -r --include=*.Rmd }\StringTok{'title:'}\NormalTok{ *}
\end{Highlighting}
\end{Shaded}

\end{frame}

\begin{frame}[fragile]{\texttt{sed}: String Editor}

\begin{itemize}
\tightlist
\item
  sed: Stream EDitor.
\item
  \url{https://www.freebsd.org/cgi/man.cgi?query=sed\&sektion=\&n=1}.
\item
  \url{https://www.gnu.org/software/sed/manual/sed.html}.
\item
  \url{https://www.gnu.org/software/sed/manual/sed.pdf} (81 páginas).
\item
  \url{http://www.grymoire.com/Unix/Sed.html}.
\item
  Permite processar texto dentro de um diretório.

  \begin{itemize}
  \tightlist
  \item
    Trocar \emph{Walmes Zeviani} por \emph{Prof.~Walmes Zeviani} em
    todos os arquivos de texto.
  \end{itemize}
\item
  Muito rápido em execução.
\end{itemize}

\begin{verbatim}
sed OPCOES... [REGEX] [ENTRADA...]
      |          |       |
      |          |       ` entradas de texto
      |          ` instrução regex
      ` opções que variam o processamento
\end{verbatim}

\end{frame}

\begin{frame}[fragile]{Exemplos de recursos do \texttt{sed}}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Mostra só a primeira linha.}
\FunctionTok{sed}\NormalTok{ -n }\StringTok{'1p'}\NormalTok{ ../data/toda-forma-de-amor.txt}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Eu não pedi pra nascer
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Mostra intervalo de linhas.}
\FunctionTok{sed}\NormalTok{ -n }\StringTok{'1,5p'}\NormalTok{ ../data/toda-forma-de-amor.txt}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## Eu não pedi pra nascer
## Eu não nasci pra perder
## Nem vou sobrar de vítima
## Das circunstâncias
\end{verbatim}

\end{frame}

\begin{frame}[fragile]{Exemplos de recursos do \texttt{sed}}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Linhas com ocorrência.}
\FunctionTok{sed}\NormalTok{ -n }\StringTok{'/a gente/p'}\NormalTok{ ../data/toda-forma-de-amor.txt}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## E a gente vive junto
## E a gente se dá bem
## E a gente vai à luta
## E a gente vive junto
## E a gente se dá bem
## E a gente vai à luta
\end{verbatim}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Encontra e substitui.}
\FunctionTok{sed} \StringTok{'s/a gente/A GENTE/g'}\NormalTok{ ../data/toda-forma-de-amor.txt }\KeywordTok{|} \FunctionTok{tail}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## E só traz o que quer
## Eu sou teu homem
## Você é minha mulher
## 
## E A GENTE vive junto
## E A GENTE se dá bem
## Não desejamos mal a quase ninguém
## E A GENTE vai à luta
## E conhece a dor
## Consideramos justa toda forma de amor
\end{verbatim}

\end{frame}

\begin{frame}[fragile]{Exemplos de recursos do \texttt{sed}}

\begin{Shaded}
\begin{Highlighting}[]
\CommentTok{# Adiciona virgula no final de cada linha.}
\FunctionTok{cp}\NormalTok{ ../data/toda-forma-de-amor.txt teste.txt}
\FunctionTok{sed}\NormalTok{ -i }\StringTok{'$!s/$/,/'}\NormalTok{ teste.txt}

\CommentTok{# Adiciona um [ na primeira linha.}
\CommentTok{# sed -i '1s/^/[/' teste.txt}
\FunctionTok{sed}\NormalTok{ -i }\StringTok{'1i ['}\NormalTok{ teste.txt}

\CommentTok{# Remove a vírgula e adiciona um ] na última linha.}
\CommentTok{# sed -i '$s/,$/]/' teste.txt}
\BuiltInTok{echo} \StringTok{"]"} \OperatorTok{>>}\NormalTok{ teste.txt}

\FunctionTok{head}\NormalTok{ -n 3 teste.txt}
\FunctionTok{tail}\NormalTok{ -n 3 teste.txt}

\CommentTok{# sed '1i [' toda-forma-de-amor.txt}
\CommentTok{# sed '$si ]' toda-forma-de-amor.txt}
\end{Highlighting}
\end{Shaded}

\begin{verbatim}
## [
## Eu não pedi pra nascer,
## Eu não nasci pra perder,
## E conhece a dor,
## Consideramos justa toda forma de amor
## ]
\end{verbatim}

\end{frame}

\begin{frame}[fragile]{\texttt{awk}: pattern-directed scanning and
processing language}

\begin{itemize}
\tightlist
\item
  \texttt{awk}: abreviação dos autores Aho, Weinberger e Kernighan.
\item
  Escrito em 1977 no AT\&T Bell Laboratories.
\item
  GNU awk (gwak) é a versão mais popular.
\item
  Paul Rubin escreveu o gawk em 1986.
\item
  \texttt{awk} significa o programa e a linguagem (assim como o R).
\item
  \texttt{awk} procura e processa linhas de texto em arquivos.
\item
  \url{https://www.freebsd.org/cgi/man.cgi?query=awk}.
\item
  \url{https://www.gnu.org/software/gawk/manual/gawk.pdf} (540 páginas).
\item
  \url{https://www.math.utah.edu/docs/info/gawk_19.html}.
\end{itemize}

Usei para processar microdados do ENEM.

\begin{itemize}
\tightlist
\item
  \url{http://portal.inep.gov.br/microdados}.
\item
  Enem 2015 é um ZIP com 1.1GB.
\end{itemize}

\end{frame}

\begin{frame}{Livros}

\begin{itemize}
\tightlist
\item
  DOUGHERTY; ROBBINS (1997)

  \begin{itemize}
  \tightlist
  \item
    sed \& awk: UNIX Power Tools.
  \item
    \url{https://books.google.com.br/books?id=Xu0G31e-4gIC}.
  \end{itemize}
\item
  ROBBINS (2001)

  \begin{itemize}
  \tightlist
  \item
    Effective Awk Programming: Text Processing and Pattern Matching.
  \item
    \url{https://books.google.com.br/books?id=blLqsSsuoUcC}.
  \end{itemize}
\end{itemize}

\end{frame}

\begin{frame}[fragile]{Resumo}

\begin{itemize}
\tightlist
\item
  Expressões regulares são \textbf{indispensáveis} para tratamento de
  texto.
\item
  O R possui utilidades para \emph{regex} no pacote \emph{base}.
\item
  O pacote \emph{stringr} contém wrappers para processamento de texto.
\item
  No Linux, \texttt{sed} e \texttt{awk} são úteis para trabalhar lotes
  de arquivos e arquivos gigantes.
\item
  O cientista de dados, principalmente o que irá trabalhar com texto,
  deve ser \textbf{faixa preta em regex}.
\end{itemize}

\end{frame}

\begin{frame}{Próxima semana}

\begin{itemize}
\tightlist
\item
  Não teremos aula na próxima semana: canaval.
\item
  Não teremos aula na semana 11-15/03: cursos na UFLA.
\item
  Não teremos aula na semana 18-22/03: curso na UFV.
\item
  Nestas 3 semanas serão indicados materiais para leitura no moodle e
  atividades avaliativas baseado nos materiais indicados.
\end{itemize}

\end{frame}

\begin{frame}{Referências}

\hypertarget{refs}{}
\hypertarget{ref-dougherty1997sed}{}
DOUGHERTY, D.; ROBBINS, A. \textbf{sed \& awk: UNIX Power Tools}.
O'Reilly Media, 1997.

\hypertarget{ref-robbins2001effective}{}
ROBBINS, A. \textbf{Effective Awk Programming: Text Processing and
Pattern Matching}. O'Reilly Media, Incorporated, 2001.

\end{frame}

\end{document}
