Cs2042_Lecture5 - CS2042 Unix Tools Fall 2009 Lecture 5 David Slater dms236 at cornell.edu October 7 2009 David Slater dms236 at cornell.edu CS2042

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS2042 - Unix Tools Fall 2009 Lecture 5 David Slater dms236 at cornell.edu October 7, 2009 David Slater dms236 at cornell.edu CS2042 - Unix Tools Organization I will hand out Homework 2 at the end of class David Slater dms236 at cornell.edu CS2042 - Unix Tools And now it gets interesting... grep The purpose of grep is to print the lines that match a particular pattern. grep grep <string> [file] searches file for all lines containing <string> grep stands for global / regular expression / print Examples: grep password file prints all lines that contain the word password in the file file . What lines contain the word monster in Frankenstein? grep ’monster’ Frankenstein.txt David Slater dms236 at cornell.edu CS2042 - Unix Tools More Simple Examples Two simple ways to use grep are on a file and on piped input: grep on a file grep "chromium" /var/log/dpkg.log Shows when I have updated chromium-browser grep piped input history | grep grep When have I used grep recently? David Slater dms236 at cornell.edu CS2042 - Unix Tools Grep options grep -i- ignores case grep -A 20 -B 10- prints the 10 lines before and 20 lines after each match grep -v- inverts the match grep -o- shows only the matched substring grep -n- displays the line number Example: grep -v # bashscript Prints all noncommented lines David Slater dms236 at cornell.edu CS2042 - Unix Tools Regular Expression grep like many programs takes in a regular expression as its input. Pattern matching with regular expressions is more sophisticated than shell expansion and also uses different syntax. More precisely, a regular expression is a set of strings; these strings match the expression. When we use regular expressions, it is (usually) best to enclose them in single quotes to stop the shell from expanding it before passing it to grep or other tools. David Slater dms236 at cornell.edu CS2042 - Unix Tools Regular Expression Notes: Regular Expressions are used all over the place. We’ve already seen grep , which takes RegExp search strings. Later we’ll see some other fun commands which will use them....
View Full Document

This note was uploaded on 03/15/2010 for the course CS 2024 taught by Professor Dinapoli during the Fall '07 term at Cornell University (Engineering School).

Page1 / 29

Cs2042_Lecture5 - CS2042 Unix Tools Fall 2009 Lecture 5 David Slater dms236 at cornell.edu October 7 2009 David Slater dms236 at cornell.edu CS2042

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online