Grep grep allows you to search one or more files for particular character patterns. The general format of this command is grep pattern files Every line.

Slides:



Advertisements
Similar presentations
การใช้ระบบปฏิบัติการ UNIX พื้นฐาน บทที่ 4 File Manipulation วิบูลย์ วราสิทธิชัย นักวิชาการคอมพิวเตอร์ ศูนย์คอมพิวเตอร์ ม. สงขลานครินทร์ เวอร์ชั่น 1 วันที่
Advertisements

COMP234 Perl Printing Special Quotes File Handling.
The Unix File System. What are the three parts of every file on a Unix filesystem? And where is each stored? Filename - stored in directories Inode -
7 Searching and Regular Expressions (Regex) Mauro Jaskelioff.
Lab6 CPCS361 I.Mona Alshehri. Working with Files and Directories Creating files Create a file with the cat command type the command cat > name_of_file.
CS 497C – Introduction to UNIX Lecture 29: - Filters Using Regular Expressions – grep and sed Chin-Chih Chang
CS 497C – Introduction to UNIX Lecture 22: - The Shell 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
Now, return to the Unix Unix shells: Subshells--- Variable---1. Local 2. Environmental.
Introduction to Unix (CA263) Decisions, Decisions By Tariq Ibn Aziz Dammam Community College.
Guide To UNIX Using Linux Third Edition
Introduction to Unix (CA263) Introduction to Shell Script Programming By Tariq Ibn Aziz.
QUOTATION This chapter teaches you about a unique feature of the shell programming language: the way it interprets quote characters. Basically, the shell.
UNIX Filters.
Shell Script Examples.
2 $ command Command Line Options ls –a –l hello hi Command Arguments.
Chapter 4: UNIX File Processing Input and Output.
COMP1070/2002/lec4/H.Melikian COMP1070 Lecture #5  Files and directories in UNIX  Various types of files  File attributes  Notion of pathname  Commands.
Advanced File Processing
Overview of the grep Command Alex Dukhovny CS 265 Spring 2011.
Introduction to Shell Script Programming
Agenda User Profile File (.profile) –Keyword Shell Variables Linux (Unix) filters –Purpose –Commands: grep, sort, awk cut, tr, wc, spell.
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.
Introduction to Unix (CA263) File Processing. Guide to UNIX Using Linux, Third Edition 2 Objectives Explain UNIX and Linux file processing Use basic file.
An Introduction to Unix Shell Scripting
More Command Line Options Pipes, Redirection, Standard files Copyright © 2015 Curt Hill.
Sed sed is a program used for editing data. It stands for stream editor. Unlike ed, sed cannot be used interactively. However, its commands are similar.
Regular expressions Used by several different UNIX commands, including ed, sed, awk, grep A period ‘.’ matches any single characters.X. matches any X.
1 Lecture 5 Additional useful commands COP 3353 Introduction to UNIX.
Shell Script Programming. 2 Using UNIX Shell Scripts Unlike high-level language programs, shell scripts do not have to be converted into machine language.
Additional UNIX Commands. 222 Lecture Overview  Multiple commands and job control  More useful UNIX utilities.
Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command to search for.
UNIX Shell Script (1) Dr. Tran, Van Hoai Faculty of Computer Science and Engineering HCMC Uni. of Technology
Chapter Five Advanced File Processing Guide To UNIX Using Linux Fourth Edition Chapter 5 Unix (34 slides)1 CTEC 110.
Chapter Five Advanced File Processing. 2 Objectives Use the pipe operator to redirect the output of one command to another command Use the grep command.
(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.
Introduction to Unix – CS 21 Lecture 6. Lecture Overview Homework questions More on wildcards Regular expressions Using grep Quiz #1.
January 23, 2007Spring Unix Lecture 2 Special Characters for Searches & Substitutions Shell Scripts Hana Filip.
Agenda Regular Expressions (Appendix A in Text) –Definition / Purpose –Commands that Use Regular Expressions –Using Regular Expressions –Using the Replacement.
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.
Introduction to Unix (CA263) File Processing (continued) By Tariq Ibn Aziz.
Shell Advanced Features. Module 8 Shell Advanced Features ♦ Introduction In Linux systems, the shells are often referred to as command line interfaces.
Introduction to Unix – CS 21 Lecture 7. Lecture Overview Regular expressions revisited emacs versus vi Basic emacs and vi usage.
Chapter Five Advanced File Processing. 2 Lesson A Selecting, Manipulating, and Formatting Information.
Introduction to Linux Instructor: Bennett M. Tanyag PART – 3 Unit 1.
Searching and Sorting. Why Use Data Files? There are many cases where the input to the program may come from a data file.Using data files in your programs.
Introduction to Unix (CA263) Passing Arguments By Tariq Ibn Aziz Dammam Community College.
1 Lecture 9 Shell Programming – Command substitution Regular expressions and grep Use of exit, for loop and expr commands COP 3353 Introduction to UNIX.
Introduction to Unix (CA263) Quotes By Tariq Ibn Aziz.
Finding Things Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Flow Control. The order in which commands execute in a shell script is called the flow of the script. When you change the commands that execute based.
Compunet Corporation Introduction to Unix (CA263) Round and Round By Tariq Ibn Aziz Dammam Community College.
FILTERS USING REGULAR EXPRESSIONS – grep and sed.
CSC 352– Unix Programming, Fall 2011 November 8, 2011, Week 11, a useful subset of regular expressions, grep and sed, parts of Chapter 11.
Linux Tutorial Lesson Two *Getting Help in Linux *Data movement and manipulation *Relative and Absolute path *Processes Note: see chapter 1,2,3 from Linux.
Shell scripts – part 1 Cs 302. Shell scripts  What is Shell Script? “Shell Script is series of command written in plain text file. “  Why to Write Shell.
CSC 352– Unix Programming, Spring 2016
Bash Introduction (adapted from chapters 1 and 2 of bash Cookbook by Albing, Vossing, & Newham) CPTE 440 John Beckett.
Lecture 9 Shell Programming – Command substitution
INTRODUCTION TO UNIX: The Shell Command Interface
CSC 352– Unix Programming, Fall 2012
The ‘grep’ Command Colin Masterson.
Guide To UNIX Using Linux Third Edition
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 grep Familiy of Commands
Introduction to Unix (CA263) Passing Arguments
Lecture 5 Additional useful commands COP 3353 Introduction to UNIX 1.
Presentation transcript:

grep grep allows you to search one or more files for particular character patterns. The general format of this command is grep pattern files Every line of each file that contains pattern is displayed at the terminal. If more than one file is specified to grep, each line is also immediately preceded by the name of the file, thus enabling you to identify the particular file that the pattern was found in.

Let's say that you want to find every occurrence of the word shell in the file ed.cmd: $ grep shell ed.cmd files, and is independent of the shell. to the shell, just type in a q. This output indicates that two lines in the file ed.cmd contain the word shell. If the pattern does not exist in the specified file(s), the grep command simply displays nothing: $ grep cracker ed.cmd

$ cat phone_book Alice Chebba Barbara Swingle Jeff Goldberg Liz Stachiw Susan Goldberg Tony Iannino When you need to look up a particular phone number, the grep command comes in handy: $ grep Susan phone_book Susan Goldberg

The grep command is useful when you have a lot of files and you want to find out which ones contain certain words or phrases. The following example shows how the grep command can be used to search for the word shell in all files in the current directory $ grep shell * cmdfiles:shell that enables sophisticated ed.cmd:files, and is independent of the shell. ed.cmd:to the shell, just type in a q. grep.cmd:occurrence of the word shell: grep.cmd:$ grep shell * grep.cmd:every use of the word shell.

It's generally a good idea to enclose your grep pattern inside a pair of single quotes to "protect" it from the shell. For instance, if you want to find all the lines containing asterisks inside the file stars, typing grep * stars does not work as expected because the shell sees the asterisk and automatically substitutes the names of all the files in your current directory! $ grep '*' stars The asterisk (*) is a special character that *********** 5 * 4 = 20

The -v Option Sometimes you're interested not in finding the lines that contain a specified pattern, but those that don't. To do this with grep is simple: You use the -v option. In the next example, grep is used to find all the lines in intro that don't contain the characters UNIX. $ grep -v 'UNIX' intro Print all lines that don't contain UNIX Thompson and Dennis Ritchie at Bell Laboratories in the late 1960s. One of the primary goals in environment that promoted efficient program development.

The -l Option At times, you may not want to see the actual lines that match a pattern but may be interested in knowing only the names of the files that contain the pattern. For example, suppose that you have a set of C programs in your current directory (these filenames end with the characters.c), and you want to know which files use a variable called Move_history. The following example shows one way of finding the answer:

$ grep 'Move_history' *.c Find Move_history in all C source files exec.c:MOVE Move_history[200] = {0}; exec.c: cpymove(&Move_history[Number_half_moves -1], exec.c: undo_move(&Move_history[Number_half_moves-1],; exec.c: cpymove(&last_move,&Move_history[Number_half_moves-1]); exec.c: convert_move(&Move_history[Number_half_moves-1]), exec.c: convert_move(&Move_history[i-1]), exec.c: convert_move(&Move_history[Number_half_moves-1]), makemove.c:IMPORT MOVE Move_history[]; makemove.c: if ( Move_history[j].from != BOOK (i,j,from) OR makemove.c: Move_history[j].to != BOOK (i,j,to) ) testch.c:GLOBAL MOVE Move_history[100] = {0}; testch.c: Move_history[Number_half_moves-1].from = move.from; testch.c: Move_history[Number_half_moves-1].to = move.to;

The -n Option If the -n option is used with grep, each line from the file that matches the specified pattern is preceded by its relative line number in the file. From previous examples, you saw that the file testch.c was one of the three files that referenced the variable Move_history; the following shows how you can pinpoint the precise lines in the file that reference the variable: $ grep -n 'Move_history' testch.c Precede matches with line numbers 13:GLOBAL MOVE Move_history[100] = {0}; 197: Move_history[Number_half_moves-1].from = move.from; 198: Move_history[Number_half_moves-1].to = move.to;