[Insight-users] bash script to search example files for terms

Darren Weber darren.weber.lists at gmail.com
Tue Oct 13 14:28:20 EDT 2009


Hi Luis,

This version below uses 'grep' options to replace the 'find' utility; the
grep options provide a case-insensitive search (-i) and recursion through
directories (-r), with an include pattern to identify file types (.cxx,
.tcl, .py).  The script now has a 'continue' statement if the itkPath
doesn't exist.  (I don't have commit rights to the cvs.)

#####BEGIN SCRIPT
#!/bin/bash

if [ $# -lt 1 ]; then
    echo "$0 'search term' ['search term' ...]"
    exit 1
fi

itkExamplePath="/opt/local/share/InsightToolkit/examples"
itkTestingPath="/opt/local/share/InsightToolkit/testing"

for term in $@; do
    echo
    echo "Search term: ${term}"
    for itkPath in "${itkExamplePath}" "${itkTestingPath}"; do
        if [ ! -d ${itkPath} ]; then
            echo "Path not found: ${itkPath}"
            echo "Modify script at $0"
            continue
        fi
        echo "Searching ITK files in: ${itkPath}"
        grep -l -E -i -r --include='*.cxx' --regexp=${term} ${itkPath}
        grep -l -E -i -r --include='*.tcl' --regexp=${term} ${itkPath}
        grep -l -E -i -r --include='*.py'  --regexp=${term} ${itkPath}
    done
done
#####END SCRIPT



This is an example run on my OSX-MacPorts system (with InsightToolkit @
3.16.0):

$ itkSearchExamples.bash 'itkImageRegistrationMethod'
Search term: itkImageRegistrationMethod
Searching ITK files in: /opt/local/share/InsightToolkit/examples
/opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration12.cxx
/opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration13.cxx
/opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration14.cxx
/opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration15.cxx
/opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration4.cxx
/opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration6.cxx
/opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration7.cxx
/opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration8.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration1.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration10.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration11.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration12.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration13.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration14.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration15.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration16.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration17.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration18.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration1o.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration2.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration2o.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration3.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration3o.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration4.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration4o.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration5.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration5o.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration6.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration6o.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration7.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration7o.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration8.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration9.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistrationHistogramPlotter.cxx
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration3.tcl
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration4.tcl
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration5.tcl
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration3.py
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration4.py
/opt/local/share/InsightToolkit/examples/Registration/ImageRegistration5.py
Searching ITK files in: /opt/local/share/InsightToolkit/testing
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkAlgorithmsHeaderTest.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkAlgorithmsPrintTest4.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkAlgorithmsTests.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_1.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_10.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_11.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_12.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_13.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_14.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_15.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_16.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_2.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_3.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_4.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_5.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_6.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_7.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_8.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkImageRegistrationMethodTest_9.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkMultiResolutionImageRegistrationMethodTest_1.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkMultiResolutionImageRegistrationMethodTest_2.cxx
/opt/local/share/InsightToolkit/testing/Code/Algorithms/itkPointSetToImageRegistrationTest_1.cxx



Regards,
Darren







On Mon, Oct 12, 2009 at 6:40 PM, Luis Ibanez <luis.ibanez at kitware.com>wrote:

> Hi Darren,
>
> Thanks for the script.
>
> It has now been committed to the CVS repository as
>
>         Insight/Examples/SearchScript.sh
>
> ---
>
> The second script,
> are you suggesting it as a separate script ?
> or as an improvement to the first one ?
>
>
>    Please let us know,
>
>
>          Thanks
>
>
>                Luis
>
>
> ----------------------------
> On Mon, Oct 12, 2009 at 4:15 PM, Darren Weber
> <darren.weber.lists at gmail.com> wrote:
> >
> > Hi Luis,
> >
> > Please feel free to distribute it with the LGPL license (or a BSD
> license,
> > or something that is compatible with the ITK license and general OSI
> > principles).
> >
> > For a more portable solution, the itk*Path variables might be obtained
> > automatically (how?).  The search path could be an input parameter (but
> that
> > almost defeats the convenience of this bash wrapper around a grep
> process).
> >
> > The grep command options list the files where the search term is found
> (not
> > the lines of those files where the search term occurs).  For a bit more
> > value, it's possible to modify the script so that the entire grep loop
> > returns an array of file paths, which could be piped into something like:
> > less -p ${searchTerm} ${fileArray}[@]
> > Then less would provide the means to page through the files.
> >
> > Here is another variation on the same thing - this one searches the
> testing
> > path and it includes the .tcl and .py example files in the search.  It
> might
> > be called 'itkSearchFiles.bash' ?
> >
> > ###### BEGIN SCRIPT
> > #!/bin/bash
> >
> > if [ $# -lt 1 ]; then
> >     echo "$0 'search term' ['search term' ...]"
> >     exit 1
> > fi
> >
> > itkExamplePath="/opt/local/share/InsightToolkit/examples"
> > itkTestingPath="/opt/local/share/InsightToolkit/testing/Code"
> >
> > for term in $@; do
> >     echo
> >     echo "Search term: ${term}"
> >     for itkPath in "${itkExamplePath}" "${itkTestingPath}" ; do
> >         if [ ! -d ${itkPath} ]; then
> >             echo "Path not found: ${itkPath}"
> >         fi
> >         echo "Searching ITK files in: ${itkPath}"
> >         grep -l -E -e ${term} ${itkPath}/*/*.cxx
> >         grep -l -E -e ${term} ${itkPath}/*/*.tcl
> >         grep -l -E -e ${term} ${itkPath}/*/*.py
> >     done
> > done
> > ###### END SCRIPT
> >
> >
> > Take care,
> > Darren
> >
> >
> >
> >
> >
> >
> > On Mon, Oct 12, 2009 at 12:26 PM, Luis Ibanez <luis.ibanez at kitware.com>
> > wrote:
> >>
> >> Hi Darren,
> >>
> >> This is really useful !
> >>
> >> Thank you for sharing it.
> >>
> >> Do you see any reason for not adding this to the CVS
> >> repository and distribute it along with ITK ?
> >>
> >> Please let us know,
> >>
> >>
> >>    Thanks
> >>
> >>
> >>          Luis
> >>
> >>
> >> ---------------------------------------
> >> On Mon, Oct 12, 2009 at 2:15 PM, Darren Weber
> >> <darren.weber.lists at gmail.com> wrote:
> >> >
> >> > This is a short bash script (grep does the work) to search for terms
> in
> >> > the
> >> > InsightToolkit example files.  It's a convenience wrapper that stores
> >> > the
> >> > path to the example files (.cxx in this case), the syntax of the grep
> >> > call,
> >> > and allows multiple terms to be given at once.  Modify the example
> >> > search
> >> > path for your purposes (it could be modified to search for the tcl or
> >> > python
> >> > examples too).  Modify the grep call as you like.
> >> >
> >> > #### BEGIN SCRIPT
> >> > #!/bin/bash
> >> >
> >> > if [ $# -lt 1 ]; then
> >> >     echo "$0 'search term' ['search term' ...]"
> >> >     exit 1
> >> > fi
> >> >
> >> > # Search the CXX files
> >> > itkExamplePath="/opt/local/share/InsightToolkit/examples/*/*.cxx"
> >> > echo "Searching ITK .cxx files in: ${itkExamplePath}"
> >> >
> >> > for term in $@; do
> >> >     echo
> >> >     echo "Search term: ${term}"
> >> >     grep -l -E -e ${term} ${itkExamplePath}
> >> > done
> >> > #### END SCRIPT
> >> >
> >> > This is an example call on my platform:
> >> >
> >> > $ itkSearchExamples.bash 'TransformWriter' 'TransformReader'
> >> > Searching ITK .cxx files in:
> >> > /opt/local/share/InsightToolkit/examples/*/*.cxx
> >> >
> >> > Search term: TransformWriter
> >> > /opt/local/share/InsightToolkit/examples/IO/TransformReadWrite.cxx
> >> >
> >> > Search term: TransformReader
> >> > /opt/local/share/InsightToolkit/examples/IO/TransformReadWrite.cxx
> >> >
> >> >
> /opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration14.cxx
> >> >
> >> >
> /opt/local/share/InsightToolkit/examples/Registration/DeformableRegistration8.cxx
> >> >
> >> > Take care,
> >> > Darren
> >> >
> >> >
> >> > _____________________________________
> >> > Powered by www.kitware.com
> >> >
> >> > Visit other Kitware open-source projects at
> >> > http://www.kitware.com/opensource/opensource.html
> >> >
> >> > Please keep messages on-topic and check the ITK FAQ at:
> >> > http://www.itk.org/Wiki/ITK_FAQ
> >> >
> >> > Follow this link to subscribe/unsubscribe:
> >> > http://www.itk.org/mailman/listinfo/insight-users
> >> >
> >> >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20091013/abf60780/attachment-0001.htm>


More information about the Insight-users mailing list