This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: Advanced Commands

Global Regex Print

face Harry Coppock

grep is a very powerful tool that allows you to search for patterns in files. The grep utility searches any given input files, selecting lines that match one or more patterns.
By default, a pattern matches an input line if the regular expression in the pattern matches the input line.

note: This is not a tutorial on regex. For those interested I would point you towards this tutorial video and also to this regex cheat sheet

Nosy Josiah interrupts: We will cover regex in more detail later on in our course! kthxbye! *poof*

grep has the following structure.

grep [options] PATTERNS FILES
where options allows you to specify how to search, see man grep for all the options - there are many! PATTERNS is the pattern you want to search for and FILES is where you want to search for

I have detailed some useful options below:

Exercise:

cd to /vol/bitbucket/hc-teaching/grep_demo. Here there are 3 .txt files to help demonstrate grep. Run the following commands:

  • First use cat or less to view the file names.txt
  • grep "John Williams" names.txt the vanilla grep command, simply returns the lines in the file names.txt which match the string "John Williams"
  • grep -w "John Williams" names.txt only matches whole words.
  • grep -i "john williams" names.txt grep is by default case sensitive, pass the option i to make it case insensitive
  • grep -n "john williams" names.txt the option -n is useful as it returns the line numbers which matches were made.
  • grep -C 2 "john williams" names.txt this returns the match and +- 2 lines of context around the match.
  • grep -win -C 2 "john williams" names.txt remember you can always combine arguments!
  • grep -win "john williams" ./ searches all files in the directory, this however, returns an error as there is a directory in your working directory. To combat this you can do:
    • grep -win "john williams" ./*.txt utilises * which matches any content which ends with .txt therefore will not attempt to read the directory
    • grep -inr "john williams" . uses the recursive argument r to recursively search down directories.
  • grep "...-...-..." names.txt here . specifies any character, so we are trying to match the patter 3x any character - 3x any character - 3x any charater.
    • grep -P "\d{3}-\d{3}-\d{3}" names.txt if you want to use PERL compatible regex you need to specify the option -P