HPC

GREP

GREP (Global regular expression print) is a way of navigating text using regular expressions. It is similar to find and replace in a word document and has many very useful applications in computing.

The basic pattern for using a grep command is `grep

The options are:

Wildcards

There are some symbols that can be used to represent more than one thing. For example, . is used to designate any character so the command grep "b.g" filename could return lines with the words big, bag or bog.

To search for a . you have to escape the function using \.

The * is used to search for as many of the previous character as there is. For example `grep “b.*g” would return things like bag, bog and bug, but also blog, berg and betting.

Additional search terms include

Capturing and Replacing

Examples

If you want to extract particular rows from a file, that include the words “key words”, you can use the following command:

grep "key words" filename

and it will list all the lines that include those words. If you want to put those lines into a csv, use the command

grep "key words" filename > newfile.csv