The ‘grep’ Command Colin Masterson.

Slides:



Advertisements
Similar presentations
CST8177 awk. The awk program is not named after the sea-bird (that's auk), nor is it a cry from a parrot (awwwk!). It's the initials of the authors, Aho,
Advertisements

CST8177 sed The Stream Editor. The original editor for Unix was called ed, short for editor. By today's standards, ed was very primitive. Soon, sed was.
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
Regular Expressions grep
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
Regular Expressions. u A regular expression is a pattern which matches some regular (predictable) text. u Regular expressions are used in many Unix utilities.
Scripting Languages Chapter 8 More About Regular Expressions.
Filters using Regular Expressions grep: Searching a Pattern.
Regular Expressions A regular expression defines a pattern of characters to be found in a string Regular expressions are made up of – Literal characters.
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
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?
Computer Programming for Biologists Class 5 Nov 20 st, 2014 Karsten Hokamp
INFO 320 Server Technology I Week 7 Regular expressions 1INFO 320 week 7.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
Regular Expressions Regular expressions are a language for string patterns. RegEx is integral to many programming languages:  Perl  Python  Javascript.
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.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
(Stream Editor) By: Ross Mills.  Sed is an acronym for stream editor  Instead of altering the original file, sed is used to scan the input file line.
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.
Introduction to Unix – CS 21 Lecture 12. Lecture Overview A few more bash programming tricks The here document Trapping signals in bash cut and tr sed.
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.
REGEX. Problems Have big text file, want to extract data – Phone numbers (503)
Introduction to sed. Sed : a “S tream ED itor ” What is Sed ?  A “non-interactive” text editor that is called from the unix command line.  Input text.
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
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
UNIX Commands RTFM: grep(1), egrep(1) & fgrep(1) Gilbert Detillieux April 13, 2010 MUUG Meeting.
Awk- An Advanced Filter by Prof. Shylaja S S Head of the Dept. Dept. of Information Science & Engineering, P.E.S Institute of Technology, Bangalore
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.
1 Lecture 10 Introduction to AWK COP 3344 Introduction to UNIX.
© Prepared By: Razif Razali 1 TMK 265: UNIX SYSTEM CHAPTER FOUR – QUOTING IN DETAIL.
ORAFACT Text Processing. ORAFACT Searching Inside Files grep - searches for patterns within files grep [options] [[-e] pattern] filename [...] -n shows.
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.
File Processing. Introduction More UNIX commands for handling files Regular Expressions and Searching files Redirection and pipes Bash facilities.
CS 403: Programming Languages Lecture 20 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Filters and Utilities. Notes: This is a simple overview of the filtering capability Some of these commands are very powerful ▫Only showing some 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.
CSE 303 Concepts and Tools for Software Development Richard C. Davis UW CSE – 10/9/2006 Lecture 6 – String Processing.
PROGRAMMING THE BASH SHELL PART III by İlker Korkmaz and Kaya Oğuz
Lesson 5-Exploring Utilities
LINUX LANGUAGE MULTIPLE CHOICE QUESTION SET-5
Looking for Patterns - Finding them with Regular Expressions
MySQL: Part II.
CST8177 sed The Stream Editor.
Regular Expression - Intro
BASIC AND EXTENDED REGULAR EXPRESSIONS
Lecture 9 Shell Programming – Command substitution
PROGRAMMING THE BASH SHELL PART IV by İlker Korkmaz and Kaya Oğuz
Intro to PHP & Variables
What is Bash Shell Scripting?
In the last class, sed to edit an input stream and understand its addressing mechanism Line addressing Using multiple instructions Context addressing Writing.
Unix Talk #2 grep/egrep/fgrep (maybe add more to this one….)
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
An Overview of Grep and Regular Expression
Chin-Chih Chang CS 497C – Introduction to UNIX Lecture 28: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
Boolean Expressions to Make Comparisons
Regular Expressions and Grep
CSCI The UNIX System Regular Expressions
Regular Expressions grep Familiy of Commands
Lab 8: Regular Expressions
REGEX.
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
ADVANCE FIND & REPLACE WITH REGULAR EXPRESSIONS
Presentation transcript:

The ‘grep’ Command Colin Masterson

What is grep? grep is a command that searches through the input file for a specified pattern When grep finds a match to the pattern, it prints the entire line to standard output grep general structure: grep options pattern input_file_names

Options grep has a variety of options that can execute a wide range of operations once a match is found This presentation is an overview of some of the more basic/common options For all available options and more in depth explanations, please see Unix resource 3 on the course website titled “Grep command documentation” or check the man page for grep in your terminal

Matching Control ‘-i’ ‘-v’ ‘-e pattern’ Ignores case. Inverts the matching. When used, grep will print out lines that do not match the pattern ‘-e pattern’ Pattern is the pattern. This can be used to specify multiple patterns, or if the pattern starts with a ‘-’. A line only has to contain one of the patterns to be matched.

Examples using ‘-i’, ’-v’, and ‘-e’ The file below was used for these examples

General Output Control Suppress normal output and instead print out a count of matching lines for each input file ‘-l’ Suppress normal output and print the name of each file that contains at least one match ‘-L’ Suppress normal output. Print the name of each file that does not contain any matches Note: both the ‘-l’ and ‘-L’ options will stop searching a file once a match is found

Examples using ‘-c’, ’-l’, and ‘-L’ Two files were used and specified below

Output Line Prefix Control Prefixes each line of output with the line number from the input file the match was found on ‘-H’ Prefix each line of output with the input file name that the match was found in ‘-T’ Makes sure that the actual line content (or whatever content comes after the ‘-T’) lands on a tab stop

Examples using ‘-H’, ’-n’, and ‘-T’ Two files were used and specified below

Context Line Control ‘-A num’ ‘-B num’ ‘-C num’ or ‘-num’ Print num lines of trailing context after matching lines ‘-B num’ Print num lines of leading context before matching lines ‘-C num’ or ‘-num’ Print num lines of leading and trailing output context

Examples using ‘-A’, ’-B’, and ‘-C’ The file below was used for these examples

Special Characters ‘.’ The period ‘.’ matches any single character. ‘?’ The preceding item is optional and will be matched at most once. ‘*’ The preceding item will be matched zero or more times. ‘+’ The preceding item will be matched one or more times. ‘{n}’ The preceding item is matched exactly n times. ‘{n,}’ The preceding item is matched n or more times. ‘{,m}’ The preceding item is matched at most m times. ‘{n,m}’ The preceding item is matched at least n times, but not more than m times.

Examples using ‘.’, ‘*’, and ‘?’

Basic vs Extended Regular Expressions Interpret pattern as basic regular expression (BRE). This is the default. ‘-E’ Interpret pattern as extended regular expression (ERE) When using basic regular expression some special characters (like ‘?’ in the previous example) loose their special meaning and must have a ‘\’, the escape character, before them When using ERE, the escape character is unnecessary

BRE and ERE Difference Note that without the ‘\’ in the BRE call (example 3), the ‘?’ is seen as a normal character

Bracket Expressions A bracket expression is a list of characters enclosed by ‘[‘ and ‘]’. It matches any single character in the list However, if the first character in the list is ‘^’, it matches any character not in the list A range can be done by using ‘-’ in a bracket expression [0-5] is the same as [012345] Some ranges are pre-defined in character classes [:digit:] is the same as 0123456789 When using grep, the class name (including brackets) must be contained within another set of brackets

Bracket Expression Example