Control¶
1. Preparation¶
Raw Data
Run the following command to save the data.
cat <<STARWARS > star-wars.txt
Episode IV – A New Hope
Episode V – The Empire Strikes Back
Episode VI – Return of the Jedi
Episode I – The Phantom Menace
Episode II – Attack of the Clones
Episode III – Revenge of the Sith
Episode VII – The Force Awakens
Episode VIII – The Last Jedi
Episode IX – The Rise of Skywalker
STARWARS
cat <<HARRYPOTTER > hp.txt
Harry Potter and the Philosopher's Stone (2001)
Harry Potter and the Chamber of Secrets (2002)
Harry Potter and the Prisoner of Azkaban (2004)
Harry Potter and the Goblet of Fire (2005)
Harry Potter and the Order of the Phoenix (2007)
Harry Potter and the Half-Blood Prince (2009)
Harry Potter and the Deathly Hallows – Part 1 (2010)
Harry Potter and the Deathly Hallows – Part 2 (2011)
HARRYPOTTER
2. Match Control¶
Match Control Examples
Run
grep -v "I" star-wars.txt
Output
Episode V – The Empire Strikes Back
Run
grep -i jedi star-wars.txt
Output
Episode VI – Return of the Jedi
Episode VIII – The Last Jedi
Run
grep -x "Return of the Jedi" star-wars.txt
Output
Run
grep -x "Episode VI – Return of the Jedi" star-wars.txt
Output
Episode VI – Return of the Jedi
Run
grep -w edi star-wars.txt
Output
Run
grep -w Jedi star-wars.txt
Output
Episode VI – Return of the Jedi
Episode VIII – The Last Jedi
3. Output Control¶
Files
Run
grep -L 'Harry' star-wars.txt hp.txt
Output
star-wars.txt
Run
grep -l 'Harry' star-wars.txt hp.txt
Output
hp.txt
Other Output Controls
Run
grep -c 'Jedi' star-wars.txt
Output
2
Use -m
to only match specific occurences.
Run
grep -m1 'Jedi' star-wars.txt
Output
Episode VI – Return of the Jedi
Run
grep -o 'of[\ a-zA-Z]\+\{1,3\}' star-wars.txt
Output
of the Jedi
of the Clones
of the Sith
of Skywalker
Run
grep -oq 'of[\ a-zA-Z]\+\{1,3\}' star-wars.txt
Output
No output captured.
4. Prefix Control¶
Prefix
Run
grep -H 'of' star-wars.txt hp.txt
or
grep 'of' star-wars.txt hp.txt
Output
star-wars.txt:Episode VI – Return of the Jedi
star-wars.txt:Episode II – Attack of the Clones
star-wars.txt:Episode III – Revenge of the Sith
star-wars.txt:Episode IX – The Rise of Skywalker
hp.txt:Harry Potter and the Chamber of Secrets (2002)
hp.txt:Harry Potter and the Prisoner of Azkaban (2004)
hp.txt:Harry Potter and the Goblet of Fire (2005)
hp.txt:Harry Potter and the Order of the Phoenix (2007)
Run
grep -h 'of' star-wars.txt hp.txt
Output
Episode VI – Return of the Jedi
Episode II – Attack of the Clones
Episode III – Revenge of the Sith
Episode IX – The Rise of Skywalker
Harry Potter and the Chamber of Secrets (2002)
Harry Potter and the Prisoner of Azkaban (2004)
Harry Potter and the Goblet of Fire (2005)
Harry Potter and the Order of the Phoenix (2007)
Run
grep -n 'of' star-wars.txt hp.txt
Output
star-wars.txt:3:Episode VI – Return of the Jedi
star-wars.txt:5:Episode II – Attack of the Clones
star-wars.txt:6:Episode III – Revenge of the Sith
star-wars.txt:9:Episode IX – The Rise of Skywalker
hp.txt:2:Harry Potter and the Chamber of Secrets (2002)
hp.txt:3:Harry Potter and the Prisoner of Azkaban (2004)
hp.txt:4:Harry Potter and the Goblet of Fire (2005)
hp.txt:5:Harry Potter and the Order of the Phoenix (2007)
5. Context Line Control¶
Context
-A {no of lines}
will print the lines after the match.
Run
grep -i 'return' -A 1 star-wars.txt
Output
Episode VI – Return of the Jedi
Episode I – The Phantom Menace
-B {no of lines}
will print the lines before the match.
Run
grep -i 'return' -B 1 star-wars.txt
Output
Episode V – The Empire Strikes Back
Episode VI – Return of the Jedi
-C {no of lines}
will print the lines before and after the match.
Run
grep -i 'return' -C 1 star-wars.txt
Output
Episode V – The Empire Strikes Back
Episode VI – Return of the Jedi
Episode I – The Phantom Menace