Lesson 8 of 14 · 11 min
Searching Text with grep
grep is a command that reads a file one line at a time and prints every line that contains the text you ask for. The name is short for global regular expression print, and in bioinformatics you will reach for it constantly to pull records out of large text files.
Your first search
# genes.txt holds one gene symbol and its role per line:
# BRAF kinase
# MAPK1 kinase
# TP53 tumor suppressor
# egfr kinase
# IL6 cytokine
# IL6R receptor
#
# Pattern first, then the file to scan. The single quotes
# keep the shell from touching the pattern.
grep 'kinase' genes.txtBRAF kinase
MAPK1 kinase
egfr kinase
The workhorse flags
A flag is a short option, written just after grep with a dash, that changes how it behaves, and flags can also be stacked together. The everyday ones are -i to ignore uppercase and lowercase differences, -n to show line numbers, -c to count matching lines instead of printing them, and -v to invert the search and show the lines that do not match. Two more are handy: -w matches your pattern only as a whole word, and -r searches through every file inside a folder.
# -v: show every line that does NOT contain 'kinase'
grep -v 'kinase' genes.txt
# -c: just count the matching lines
grep -c 'kinase' genes.txt
# -n: put the line number in front of each match
grep -n 'kinase' genes.txt
# -w: match IL6 as a whole word, so IL6R is skipped
grep -w 'IL6' genes.txtTP53 tumor suppressor
IL6 cytokine
IL6R receptor
3
1:BRAF kinase
2:MAPK1 kinase
4:egfr kinase
IL6 cytokine
Grep for biology
# sequences.fasta holds 4 sequences. Each record is a header
# line starting with '>' followed by one line of DNA:
# >seq1
# ATGGAATTCACG
# >seq2
# TTAGAATTCGGA
# >seq3
# CGGGATCCAATT
# >seq4
# ACGTACGTACGT
#
# Only header lines contain '>', so counting them counts the sequences.
grep -c '>' sequences.fasta
# Pull the reads that carry the EcoRI site GAATTC.
# -i ignores case, so the lowercase pattern still matches uppercase DNA.
grep -i 'gaattc' sequences.fasta
# reads/ is a folder with one FASTA file per sample
# (sampleA.fasta, sampleB.fasta, ...), each a header on line 1
# and its DNA on line 2. -r searches every file in the folder,
# and -n adds the line number to each hit.
grep -rn 'GAATTC' reads/
# -E turns on extended patterns, where | means OR.
# This finds an EcoRI (GAATTC) OR a BamHI (GGATCC) site.
grep -E 'GAATTC|GGATCC' sequences.fasta4
ATGGAATTCACG
TTAGAATTCGGA
reads/sampleA.fasta:2:ATGGAATTCACG
reads/sampleB.fasta:2:TTAGAATTCGGA
ATGGAATTCACG
TTAGAATTCGGA
CGGGATCCAATT
Try it yourself: Save five short DNA reads in a file called reads.fasta, giving each one a header line that starts with '>'. Run grep -c '>' reads.fasta to count how many sequences you saved, then run grep -in 'gaattc' reads.fasta to list, with line numbers and ignoring case, every read that contains the EcoRI site GAATTC.