Skip to content

Instantly share code, notes, and snippets.

@nishadhka
Forked from jbarratt/nbgrep
Last active June 5, 2019 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nishadhka/8b381af02662130e9adf877678d32548 to your computer and use it in GitHub Desktop.
Save nishadhka/8b381af02662130e9adf877678d32548 to your computer and use it in GitHub Desktop.
'nbgrep', search the code of all your ipython notebooks for version 3 and 4
#!/bin/bash
# usage: nbgrep 'pattern'
SEARCHPATH='/home/workingnotesBack'
# 'jq' technique lifted with gratitude
# from https://gist.github.com/mlgill/5c55253a3bc84a96addf
# Break on newlines instead of any whitespace
# IPython Notebook files often have spaces in it
SAVEIFS=$IFS
IFS=$(echo -en "\n\b")
if ! type mdfind > /dev/null 2>&1; then
# Use find from findutils
FILES=$(find $SEARCHPATH -name '*.ipynb')
else
# mdfind uses OSX's spotlight search, so it's almost instant
# generate a list of all the ipynb files in any of the directories
FILES=$(mdfind -onlyin $SEARCHPATH -name '.ipynb')
fi
# On the command line we get the argument to search for
PATTERN=$1
for f in $FILES
do
# Check Notebook JSON format first
NB_VERSION=$(jq '.nbformat' $f)
if [ $NB_VERSION -eq 3 ]; then
# IPython notebook JSON format
OUTPUT=$(jq '.worksheets[]?.cells[]? | select(.cell_type=="code") | .input[]?//.input' $f \
| sed 's/^"//g;s/"$//g;s/\\n$//g;s/\\"/"/g;s/\\\\/\\/g;s/\\n/\n/g' \
| pygmentize -l python 2>/dev/null \
| grep $PATTERN)
elif [ $NB_VERSION -eq 4 ]; then
# Jupyter notebook JSON format
OUTPUT=$(jq '.cells[]? | select(.cell_type=="code") | .source[]?//.source' $f \
| sed 's/^"//g;s/"$//g;s/\\n$//g;s/\\"/"/g;s/\\\\/\\/g;s/\\n/\n/g' \
| pygmentize -l python 2>/dev/null \
| grep $PATTERN)
fi
# If the grep matched anything, print it
if [ $? -eq 0 ]; then
echo -e "$f:\n\n$OUTPUT\n\n"
fi
done
IFS=$SAVEIFS
@nishadhka
Copy link
Author

  1. Have to install jq
sudo apt-get install jq
  1. Make this file executable and run as ./nbgrep pd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment