Skip to content

Instantly share code, notes, and snippets.

@jbarratt
Last active July 31, 2025 23:11
Show Gist options
  • Save jbarratt/fa1d3473048e5f856aeb to your computer and use it in GitHub Desktop.
Save jbarratt/fa1d3473048e5f856aeb to your computer and use it in GitHub Desktop.

Revisions

  1. jbarratt revised this gist Aug 25, 2014. 1 changed file with 8 additions and 6 deletions.
    14 changes: 8 additions & 6 deletions nbgrep
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    # usage: nbgrep 'pattern'

    SEARCHPATH='~/work'
    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 2> /dev/null; then
    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 color possible)
    # 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") | 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 \

    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"
  2. jbarratt revised this gist Aug 25, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nbgrep
    Original 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") | .input[]' $f \
    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)
  3. @tomspur tomspur revised this gist Aug 22, 2014. 1 changed file with 8 additions and 3 deletions.
    11 changes: 8 additions & 3 deletions nbgrep
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -12,9 +12,14 @@ SEARCHPATH='~/work'
    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')
    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
  4. jbarratt created this gist Jul 22, 2014.
    39 changes: 39 additions & 0 deletions nbgrep
    Original 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