Last active
July 31, 2025 23:11
-
-
Save jbarratt/fa1d3473048e5f856aeb to your computer and use it in GitHub Desktop.
Revisions
-
jbarratt revised this gist
Aug 25, 2014 . 1 changed file with 8 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ # usage: nbgrep 'pattern' SEARCHPATH=~/work/ # 'jq' technique lifted with gratitude # from https://gist.github.com/mlgill/5c55253a3bc84a96addf @@ -12,7 +12,7 @@ SEARCHPATH='~/work' 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 @@ -28,13 +28,15 @@ for f in $FILES do # Use 'jq' to filter out only the code in input cells # Then remove quoting # Colorize it with pygments (give it the most context possible to get color right) # And finally, search the remainder for a given pattern 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) # If the grep matched anything, print it if [ $? -eq 0 ]; then echo -e "$f:\n\n$OUTPUT\n\n" -
jbarratt revised this gist
Aug 25, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,7 @@ do # Then remove quoting # Colorize it with pygments (give it the most color possible) # And finally, search the remainder for a given pattern OUTPUT=$(jq '.worksheets[].cells[] | select(.cell_type=="code") | if type .input == "array" then .input[] else .input end' $f \ | sed 's/^"//g;s/"$//g;s/\\n$//g;s/\\"/"/g;s/\\\\/\\/g' \ | pygmentize -l python \ | grep $PATTERN) -
tomspur revised this gist
Aug 22, 2014 . 1 changed file with 8 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,9 +12,14 @@ SEARCHPATH='~/work' SAVEIFS=$IFS IFS=$(echo -en "\n\b") if ! type mdfind 2> /dev/null; 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 -
jbarratt created this gist
Jul 22, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,39 @@ #!/bin/bash # usage: nbgrep 'pattern' SEARCHPATH='~/work' # '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") # 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') # On the command line we get the argument to search for PATTERN=$1 for f in $FILES do # Use 'jq' to filter out only the code in input cells # Then remove quoting # Colorize it with pygments (give it the most color possible) # And finally, search the remainder for a given pattern OUTPUT=$(jq '.worksheets[].cells[] | select(.cell_type=="code") | .input[]' $f \ | sed 's/^"//g;s/"$//g;s/\\n$//g;s/\\"/"/g;s/\\\\/\\/g' \ | pygmentize -l python \ | grep $PATTERN) # If the grep matched anything, print it if [ $? -eq 0 ]; then echo -e "$f:\n\n$OUTPUT\n\n" fi done IFS=$SAVEIFS