GREP basics¶
grep allows you to search within files.
see what lines in alice in wonderland contain the word watch¶
In [ ]:
Copied!
%%bash
# see what lines in alice in wonderland contain the word watch
grep watch alice.txt
%%bash
# see what lines in alice in wonderland contain the word watch
grep watch alice.txt
watch out of its waistcoat-pocket_, and looked at it, and then hurried watch to take out of it, and burning with curiosity, she ran across the is it?” he said, turning to Alice: he had taken his watch out of his The March Hare took the watch and looked at it gloomily: then he dipped funny watch!” she remarked. “It tells the day of the month, and doesn’t “Why should it?” muttered the Hatter. “Does _your_ watch tell you what went nearer to watch them, and just as she came up to them she heard things—” when his eye chanced to fall upon Alice, as she stood watching watching it a minute or two, she made it out to be a grin, and she said The Gryphon sat up and rubbed its eyes: then it watched the Queen till “Thank you, it’s a very interesting dance to watch,” said Alice, Alice watched the White Rabbit as he fumbled over the list, feeling hand, watching the setting sun, and thinking of little Alice and all
search for lines that contain the word chapter¶
In [ ]:
Copied!
%%bash
# search for lines that contain the word chapter
grep chapter alice.txt
# this returns nothing as there is no line that contains chapter all lowercase.
%%bash
# search for lines that contain the word chapter
grep chapter alice.txt
# this returns nothing as there is no line that contains chapter all lowercase.
--------------------------------------------------------------------------- CalledProcessError Traceback (most recent call last) <ipython-input-5-dfb6264d31bc> in <module> ----> 1 get_ipython().run_cell_magic('bash', '', '\ngrep chapter alice.txt\n') /shared-libs/python3.7/py-core/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2397 with self.builtin_trap: 2398 args = (magic_arg_s, cell) -> 2399 result = fn(*args, **kwargs) 2400 return result 2401 /shared-libs/python3.7/py-core/lib/python3.7/site-packages/IPython/core/magics/script.py in named_script_magic(line, cell) 140 else: 141 line = script --> 142 return self.shebang(line, cell) 143 144 # write a basic docstring: /shared-libs/python3.7/py-core/lib/python3.7/site-packages/decorator.py in fun(*args, **kw) 229 if not kwsyntax: 230 args, kw = fix(args, kw, sig) --> 231 return caller(func, *(extras + args), **kw) 232 fun.__name__ = func.__name__ 233 fun.__doc__ = func.__doc__ /shared-libs/python3.7/py-core/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k) 185 # but it's overkill for just that one bit of state. 186 def magic_deco(arg): --> 187 call = lambda f, *a, **k: f(*a, **k) 188 189 if callable(arg): /shared-libs/python3.7/py-core/lib/python3.7/site-packages/IPython/core/magics/script.py in shebang(self, line, cell) 243 sys.stderr.flush() 244 if args.raise_error and p.returncode!=0: --> 245 raise CalledProcessError(p.returncode, cell, output=out, stderr=err) 246 247 def _run_script(self, p, cell, to_close): CalledProcessError: Command 'b'\ngrep chapter alice.txt\n'' returned non-zero exit status 1.
search for lines that contain the word chapter ignoring the case¶
In [ ]:
Copied!
%%bash
# search for lines that contain the word chapter ignoring the case
grep -i chapter alice.txt
%%bash
# search for lines that contain the word chapter ignoring the case
grep -i chapter alice.txt
CHAPTER I. Down the Rabbit-Hole CHAPTER II. The Pool of Tears CHAPTER III. A Caucus-Race and a Long Tale CHAPTER IV. The Rabbit Sends in a Little Bill CHAPTER V. Advice from a Caterpillar CHAPTER VI. Pig and Pepper CHAPTER VII. A Mad Tea-Party CHAPTER VIII. The Queen’s Croquet-Ground CHAPTER IX. The Mock Turtle’s Story CHAPTER X. The Lobster Quadrille CHAPTER XI. Who Stole the Tarts? CHAPTER XII. Alice’s Evidence CHAPTER I. CHAPTER II. CHAPTER III. CHAPTER IV. CHAPTER V. CHAPTER VI. CHAPTER VII. CHAPTER VIII. CHAPTER IX. CHAPTER X. CHAPTER XI. CHAPTER XII.
search for lines that begin with the word chapter ignoring the case¶
In [ ]:
Copied!
%%bash
# search for lines that begin with the word chapter ignoring the case
grep -i ^chapter alice.txt
%%bash
# search for lines that begin with the word chapter ignoring the case
grep -i ^chapter alice.txt
CHAPTER I. CHAPTER II. CHAPTER III. CHAPTER IV. CHAPTER V. CHAPTER VI. CHAPTER VII. CHAPTER VIII. CHAPTER IX. CHAPTER X. CHAPTER XI. CHAPTER XII.