Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.

Slides:



Advertisements
Similar presentations
Regular Expressions in Perl By Josue Vazquez. What are Regular Expressions? A template that either matches or doesn’t match a given string. Often called.
Advertisements

Lecture 5  Regular Expressions;  grep; CSE4251 The Unix Programming Environment.
การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 31: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
LING 388: Language and Computers Sandiway Fong Lecture 2: 8/23.
Regular Expressions. u A regular expression is a pattern which matches some regular (predictable) text. u Regular expressions are used in many Unix utilities.
Grep, comm, and uniq. The grep Command The grep command allows a user to search for specific text inside a file. The grep command will find all occurrences.
CSE467/567 Computational Linguistics Carl Alphonce Computer Science & Engineering University at Buffalo.
Filters using Regular Expressions grep: Searching a Pattern.
Regular Language & Expressions. Regular Language A regular language is one that a finite state machine (fsm) will accept. ‘Alphabet’: {a, b} ‘Rules’:
Chapter 4: UNIX File Processing Input and Output.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
Last Updated March 2006 Slide 1 Regular Expressions.
Va-scanCopyright 2002, Marchany Unit 6 – Solaris File Security Randy Marchany VA Tech Computing Center.
Regular Expression Darby Tien-Hao Chang (a.k.a. dirty) Department of Electrical Engineering, National Cheng Kung University.
System Programming Regular Expressions Regular Expressions
Exam Revision. Exam Details Time 90 minutes (1hour 30 minutes). Six questions! How long per question? Five parts per question. How long for each part?
 Text Manipulation and Data Collection. General Programming Practice Find a string within a text Find a string ‘man’ from a ‘A successful man’
Unix Talk #2 (sed). 2 You have learned…  Regular expressions, grep, & egrep  grep & egrep are tools used to search for text in a file  AWK -- powerful.
Linux+ Guide to Linux Certification, Third Edition
REGULAR EXPRESSIONS. Lexical Analysis Lexical analysers can be constructed by programs such as LEX These programs employ as input a description of the.
Regular expressions Used by several different UNIX commands, including ed, sed, awk, grep A period ‘.’ matches any single characters.X. matches any X.
CS 403: Programming Languages Fall 2004 Department of Computer Science University of Alabama Joel Jones.
1 Lecture 5 Additional useful commands COP 3353 Introduction to UNIX.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Week 3 Exploring Linux Filesystems. Objectives  Understand and navigate the Linux directory structure using relative and absolute pathnames  Describe.
Lesson 9-Setting and Using Permissions. Overview Describing file permissions. Using execute permissions with a file. Changing file permissions using mnemonics.
Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 5.1 © Copyright IBM Corporation 2008 Unit 10 Linux.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/15.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
I/O Redirection and Regular Expressions February 9 th, 2004 Class Meeting 4.
Regular Expression - Intro Patterns that define a set of strings (or, pieces of a string) Not wildcards (similar notion, but different thing) Used by utilities.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Appendix A: Regular Expressions It’s All Greek to Me.
May 2008CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
I/O Redirection & Regular Expressions CS 2204 Class meeting 4 *Notes by Doug Bowman and other members of the CS faculty at Virginia Tech. Copyright
Advanced Text Processing. 222 Lecture Overview  Character manipulation commands cut, paste, tr  Line manipulation commands sort, uniq, diff  Regular.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Unix Programming Environment Part 3-4 Regular Expression and Pattern Matching Prepared by Xu Zhenya( Draft – Xu Zhenya(
Regular Expressions CS 2204 Class meeting 6 Created by Doug Bowman, 2001 Modified by Mir Farooq Ali, 2002.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
CSCI 330 UNIX and Network Programming Unit IV Shell, Part 2.
Validation using Regular Expressions. Regular Expression Instead of asking if user input has some particular value, sometimes you want to know if it follows.
What is grep ?  % man grep  DESCRIPTION  The grep utility searches text files for a pattern and prints all lines that contain that pattern. It uses.
FILTERS USING REGULAR EXPRESSIONS – grep and sed.
-Joseph Beberman *Some slides are inspired by a PowerPoint presentation used by professor Seikyung Jung, which was derived from Charlie Wiseman.
May 2006CLINT-LIN Regular Expressions1 Introduction to Computational Linguistics Regular Expressions (Tutorial derived from NLTK)
ICS611 Lex Set 3. Lex and Yacc Lex is a program that generates lexical analyzers Converting the source code into the symbols (tokens) is the work of the.
Awk 2 – more awk. AWK INVOCATION AND OPERATION the "-F" option allows changing Awk's "field separator" character. Awk regards each line of input data.
CIRC Summer School 2016 Baowei Liu
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Lesson 5-Exploring Utilities
Looking for Patterns - Finding them with Regular Expressions
CIRC Winter Boot Camp 2017 Baowei Liu
Regular Expression - Intro
BASIC AND EXTENDED REGULAR EXPRESSIONS
CS 403: Programming Languages
The ‘grep’ Command Colin Masterson.
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Regular Expressions and Grep
CSCI The UNIX System Regular Expressions
Regular Expressions grep Familiy of Commands
Lab 8: Regular Expressions
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Presentation transcript:

Overview of the grep Command Alex Dukhovny CS 265 Spring 2011

What is grep Command grep - "general regular expression parser“ Search command for UNIX. Used to search for text strings and regular expressions within one or more files. man grep

Common grep Command Options grep [options] pattern [files] -b Display the block number at the beginning of each line. -c Display the number of matched lines. -h Display the matched lines, but do not display the filenames. -i Ignore case sensitivity. -l Display the filenames, but do not display the matched lines. -n Display the matched lines and their line numbers. -s Silent mode. -v Display all lines that do NOT match. -w Match whole word. grep -c Alex my_file.htm

How to use grep command Search file for a user – $ grep ad85 /etc/passwd Search file ignoring word case – $ grep -i “ad85" /etc/passwd Search recursively all files and directories under given directory – $ grep -r “ad85" /etc/

How to use grep command Search for a specific word in file – $ grep -w “alex" $HOME/cs265.htm Search for 2 different words in file – $ grep -w ‘alex|victoria' $HOME/cs265.htm Count lines that matched in file – $ grep -c 'word' $HOME/cs265.htm

How to use grep command Display lines that did not match a pattern – $ grep -v cs265 $HOME/cs265.htm Number of lines that contain matched pattern – $ grep -n 'word' $HOME/cs265.htm Display filenames that matched pattern, but not lines from the files – $ grep -l ‘word' *.htm

grep and Wildcards Dot (. ) – matches 1 character Asterisks ( * ) – matches multiple characters Examples: – grep b.g myfile  finds the words “big”, “bag” – grep b*k myfile  finds the word “back”, “buck”, “book”

grep and Regular Expressions A "regular expression" is a pattern that describes a set of strings. Regular expressions are used when you want to search for specific lines of text containing a particular pattern.

grep and Regular Expressions ^ (Caret) = match expression at the start of a line, as in ^A. $ (Dollar Sign) = match expression at the end of a line, as in A$. \ (Back Slash) = turn off the special meaning of the next character, as in \^. [ ] (Brackets) = match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a range, as in [0-9]. [^ ] = match any one character except those enclosed in [ ], as in [^0-9].

grep and Regular Expressions. (Period) = match a single character of any value, except end of line. * (Asterisk) = match zero or more of the preceding character or expression. \{x,y\} = match x to y occurrences of the preceding. \{x\} = match exactly x occurrences of the preceding. \{x,\} = match x or more occurrences of the preceding.

grep and Regular Expressions grep bob files {search files for lines with ‘bob'} grep '^bob' files {‘bob' at the start of a line} grep ‘bob$' files {‘bob' at the end of a line} grep '^bob$' files {lines containing only ‘bob'} grep '\^b' files {lines starting with '^b', "\" escapes the ^} grep '[Bb]mug' files {search for ‘Bob' or ‘bob'} grep 'B[oO][bB]' files {search for BOB, Bob, BOb or BoB } grep '^$' files {search for empty lines} grep '[0-9][0-9]' files {search for pairs of numeric digits}

grep and Regular Expressions Questions ?