%% $Header: /cvsroot/lcdp/lpic/general-linux-1/slides/gl1.103.7.slides.tex,v 1.2 2003/05/30 05:11:28 waratah Exp $ \input{gl1.slide-header.tex} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \begin{document} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %============================================================================== \begin{slide} %ghr \listofslides \vfill \end{slide} %------------------------------------------------------------------------------ %----10->|-----20->|-----30->|-----40->|-----50->|-----60->|-----70->|-----80-> \begin{slide} %================================================================ \begin{center} \LARGE \textsf{-- General Linux 1 -- \\ Search text files using regular expressions} \LARGE \textsf{[] } \\[2mm] \large \textsf{(Linux Professional Institute Certification)}\\[1mm] \normalsize\end{center} \footnote{Copyright \copyright\ 2002 Geoffrey Robertson. Permission is granted to make and distribute verbatim copies or modified versions of this document provided that this copyright notice and this permission notice are preserved on all copies under the terms of the GNU General Public License as published by the Free Software Foundation---either version 2 of the License or (at your option) any later version.} \scriptsize \begin{verbatim} .~. /V\ by: geoffrey robertson // \\ geoffrey@zip.com.au @._.@ \end{verbatim} \tiny \begin{verbatim} $Id: gl1.103.7.slides.tex,v 1.2 2003/05/30 05:11:28 waratah Exp $ \end{verbatim} \normalsize \vfill \end{slide} %----------------------------------------------------------- %----10->|-----20->|-----30->|-----40->|-----50->|-----60->|-----70->|-----80-> %============================================================================== \begin{slide}{} %ghr \Slidecontents \vfill \end{slide} %----------------------------------------------------------- %============================================================================== %============================================================================== \begin{slide} %ghr \slideheading{GNU \& Unix Commands} \slidesubheading{\texttt{sed}---stream editor} \begin{description} \item[1.103.1] Work on the command line \item[1.103.2] Process text streams using filters \item[1.103.3] Perform basic file management \item[1.103.4] Use streams, pipes, and redirects \item[1.103.5] Create, monitor, and kill processes \item[1.103.6] Modify process execution priorities \item[1.103.7] {\bf \underline{Search text files using regular expressions}} \end{description} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== %============================================================================== \begin{slide} %ghr \Slideheading{Search text files using regular expressions} \slidesubheading{Objective} The candidate should be able to manipulate files and text data using regular expressions. This objective includes creating simple regular expressions containing several notational elements. It also includes using regular expression tools to perform searches through a filesystem or file content. \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide} %ghr \Slideheading{Search text files using regular expressions} \slidesubheading{Key files, terms, and utilities} \begin{alltt} grep regexp sed \end{alltt} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide} %ghr \Slideheading{Search text files using regular expressions} \slidesubheading{Resources of interest} Fun with Regular Expressions by Adrian J. Chung \begin{alltt} http://thelinuxgurus.org/regexp.html \end{alltt} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== %============================================================================== \begin{slide}{} %ghr \slideheading{\texttt{grep---}} \slidesubheading{} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== %============================================================================== \begin{slide}{} %ghr \slideheading{\texttt{regexes---}} \slidesubheading{} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== %============================================================================== \begin{slide}{} %ghr \slideheading{\texttt{sed}---\underline{\textbf{s}}tream \underline{\textbf{ed}}itor} \slidesubheading{} \begin{itemize} \item \texttt{sed} is a non-interactive, or stream oriented editor \overlay{1} \item \texttt{sed} works in a similar fashion to any text filter, typically taking the lines from a file, filtering them and sending them to \textsf{STDIO} \overlay{2} \item Example: replace ``teh'' with ``the'' \begin{alltt} \$ sed s/teh/the/g my_file.txt \(\hookleftarrow\) \end{alltt} \overlay{3} \item The original file is not touched by \textsf{sed} \overlay{4} \item Save the results: \begin{alltt} \$ sed s/teh/the/g old.txt > new.txt\(\hookleftarrow\) \end{alltt} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Calling \texttt{sed}} \begin{itemize} \item \textsf{sed} one liners: \begin{alltt} \$ sed [opts] 'sed-cmds' input-file(s) \(\hookleftarrow\) \end{alltt} \overlay{1} \item \textsf{sed} using a script file: \begin{alltt} \$ sed [opts] -f script-file input-file(s) \(\hookleftarrow\) \end{alltt} \overlay{2} \item Script with a \textsf{sed} shebang: \begin{alltt} \$ cat script.sed \(\hookleftarrow\) #!/bin/sed -f ... \end{alltt} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{The \texttt{sed} options} \begin{description} \item[-n] No print. The default is to print all lines plus lines selected with the \textsf{p} command. \overlay{1} \item[-e] The next command is an edit command; used for multiple edits. \overlay{2} \item[-f] \textsf{sed} commands are in a file. \end{description} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Finding text using \texttt{sed}} \begin{itemize} \overlay{1} \item Using line numbers: singly or in a range. \overlay{2} \item Using Regular Expressions. \overlay{3} \item Examples: \begin{description} \item[x] Where \textsf{x} is a line number \item[x,y] In a range of lines, from \textsf{x} to \textsf{y} \item[/pattern/] Where \textsf{pattern} is a regex \item[/pattern/pattern/] Choice of patterns \item[/pattern/,x] Look for the pattern on this line \item[x,/pattern/] Look only at line \textsf{x} for the pattern \item[x,y!] Not lines \textsf{x} to \textsf{y} \end{description} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Basic \textsf{sed} editing commands} \begin{description} \item[p] Print the matched lines \item[=] Display the line number of the file \item[a]\verb+\+ Append the text after the addressed line \item[i]\verb+\+ Insert new text after the addressed line \item[d] Delete addressed lines \item[c] Replace addressed text with new text \item[s] Substitute pattern with replacement pattern \item[r] Read text from another file \end{description} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Basic \textsf{sed} editing commands} \begin{description} \item[w] Write text to file \item[q] Quit after first pattern has been matched, or just quit \item[l] Show control characters in their octal ASCII equivalent \item[()] Group a series of commands to be performed only on addressed lines \item[n] Read the next line of text from another file and append it \item[g] Paste the contents of pattern2 into pattern1 \item[y] Translate characters \item[n] Append next input line; this allows pattern matching across two lines \end{description} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \overlay{} \Slideheading{\texttt{sed}---stream editor} \slidesubheading{\texttt{sed} examples} \begin{itemize} \item Print line 3 only: \begin{alltt} \$ sed -n '3p' foo.txt \(\hookleftarrow\) \end{alltt} \overlay{1} \item Print lines 5 through 8: \begin{alltt} \$ sed -n '5,8p' foo.txt \(\hookleftarrow\) \end{alltt} \overlay{2} \item Print lines with \textit{Fred} in them: \begin{alltt} \$ sed -n '/fred/p' foo.txt \(\hookleftarrow\) \end{alltt} \overlay{3} \item Search for \textit{Fred} only on line 4: \begin{alltt} \$ sed -n '4,/Fred/p' foo.txt \(\hookleftarrow\) \end{alltt} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{\texttt{sed} examples} \begin{itemize} \item Print lines containing \textsf{\$100}: \begin{verbatim} $ sed -n '/\$100/p' foo.txt \end{verbatim}%$ \overlay{1} \item Print file 10th line to the last: \begin{alltt} \$ sed -n '10,\$p' \(\hookleftarrow\) \end{alltt} \overlay{2} \item Print lines with \textsf{ing}'s in them \begin{alltt} \$ sed -n '/ing/p' \(\hookleftarrow\) \end{alltt} \overlay{3} \item Print just the line number of a match: \begin{alltt} \$ sed -n '/funny/=' foo.txt \(\hookleftarrow\) 3 \end{alltt} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{\texttt{sed} examples} \small \begin{itemize} \item Print the line and its number: \begin{alltt} \$ sed -n -e '/fun/p' -e '/fun/=' foo.txt \(\hookleftarrow\) That was funny 3 \end{alltt} \overlay{1} \item Appending text: \begin{alltt} \$ cat my.script /funny/p /funny/a\verb+\+ ha ha ha \$ sed -n -f my.script foo.txt \(\hookleftarrow\) That was funny ha ha ha \end{alltt} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{\texttt{sed} examples} \begin{itemize} \item Substitution: \begin{alltt} \$ sed -n 's/this/that/' foo.file \end{alltt} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Write a \texttt{sed} script} \begin{itemize} \item Create a script find\_the.sh that will print (append) ``Got One!'' every time it sees the word ``the'' in a file. \begin{verbatim} #!/bin/sed -f /the/ a\ Got one! \end{verbatim} \item Make the script executable: \begin{alltt} \$ chmod u+x find_the.sh \(\hookleftarrow\) \end{alltt} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Run a \texttt{sed} script} \begin{alltt} \$ ./find_the.sh owl.pussy.cat.poem \(\hookleftarrow\) "Dear Pig, are you willing to sell for one shilling Your ring?" Said the Piggy, "I will." Got one! So they took it away, and were married next day Got one! By the Turkey who lives on the hill. Got one! \end{alltt} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \slideheading{The Owl and the Pussy-Cat} \begin{verse} The Owl and the Pussy-Cat went to sea\\ In a beautiful pea-green boat:\\ They took some honey, and plenty of money\\ Wrapped up in a five-pound note.\\ The Owl looked up to the stars above,\\ And sang to a small guitar,\\ "O lovely Pussy, O Pussy, my love,\\ What a beautiful Pussy you are,\\ You are,\\ You are!\\ What a beautiful Pussy you are!" \end{verse} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{The Owl and the Pussy-Cat} \begin{verse} Pussy said to the Owl, "You elegant fowl,\\ How charmingly sweet you sing!\\ Oh! let us be married; too long we have tarried:\\ But what shall we do for a ring?"\\ They sailed away, for a year and a day,\\ To the land where the bong-tree grows;\\ And there in a wood a Piggy-wig stood,\\ With a ring at the end of his nose,\\ His nose,\\ His nose,\\ With a ring at the end of his nose. \end{verse} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{The Owl and the Pussy-Cat} \begin{verse} "Dear Pig, are you willing to sell for one shilling\\ Your ring?" Said the Piggy, "I will."\\ So they took it away, and were married next day\\ By the Turkey who lives on the hill.\\ They dined on mince and slices of quince,\\ Which they ate with a runcible spoon;\\ And hand in hand on the edge of the sand\\ They danced by the light of the moon,\\ The moon,\\ The moon,\\ They danced by the light of the moon.\\ \end{verse} Edward Lear \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Inserting test with \texttt{sed}} \small \begin{itemize} \item Create a script find\_the.sh that will print (append) ``Got One!'' every time it sees the word ``the'' in a file. \begin{verbatim} #!/bin/sed -f /Owl/ i\ Owl coming up! \end{verbatim} \item Run the script: \begin{alltt} \$ chmod u+x owl.sh \(\hookleftarrow\) \$ ./owl.sh owl.pussy.cat.poem \(\hookleftarrow\) Wrapped up in a five-pound note. Owl coming up! The Owl looked up to the stars above, \end{alltt} \end{itemize} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Answers to Questions} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Answers to Questions} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Answers to Questions} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Answers to Questions} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{Answers to Questions} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slideheading{\texttt{sed}---stream editor} \slidesubheading{} \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \Slidecontents \vfill \end{slide} %----------------------------------------------------------- %============================================================================== \begin{slide}{} %ghr \heading{The End} \Slidecontents \vfill \end{slide} %----------------------------------------------------------- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \end{document} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %============================================================================== %============================================================================== \begin{slide}{} \slideheading{} \vfill \end{slide} %----------------------------------------------------------- ============================================================================= \begin{slide}{} \end{slide} %------------------------------------------------------------------