Find The Answer Within: using grep to find lost code

A short tech-tip this week. How can you find a line of code somewhere on your computer?

I often find that I need to write a line of code and I can’t remember the exact syntax. To add to the frustration, I can remember writing a similar line before, but can’t remember in which file it was (or for what project, or even when it might’ve been). How can I quickly track that line down?

Today I needed to write a LoadWave command in Igor. These lines are difficult to remember because they use many flags to determine how to load data into Igor. This is the line I ended up with:

LoadWave/M/O/J/K=1/L={0,1,0,0,0}/P=csvDiskFolder/A=$wName/Q fileName

So, eight flags and several parameters… difficult to remember. No problem I’ve used this command before. Now where did I put it…

Grep is the answer. Using the shell I could do

grep -rli Documents/WaveMetrics/Igor\ Pro\ *\ User\ Files/User\ Procedures -e "LoadWave"

this gave me a list of all Igor procedure files that feature the LoadWave command. Using the asterisk means I search Igor 8, 7 and 6 files on my hard drive.

...
Documents/WaveMetrics/Igor Pro 8 User Files/User Procedures/GScholar.ipf
Documents/WaveMetrics/Igor Pro 8 User Files/User Procedures/CellShape.ipf
Documents/WaveMetrics/Igor Pro 8 User Files/User Procedures/FRAPKinetics.ipf
Documents/WaveMetrics/Igor Pro 8 User Files/User Procedures/IgorTunes.ipf
Documents/WaveMetrics/Igor Pro 8 User Files/User Procedures/ParseTimestampsFromOME.ipf
Documents/WaveMetrics/Igor Pro 8 User Files/User Procedures/Storm54.ipf
Documents/WaveMetrics/Igor Pro 8 User Files/User Procedures/LoadColorTableCSVs.ipf
Documents/WaveMetrics/Igor Pro 8 User Files/User Procedures/KinetochoreColoc.ipf
...

There were ~30 files – hooray! Each one features at least one LoadWave command. Opening each one to look at individually is too onerous. Let’s use the shell again. I can cd the User Procedures folder I want and then

grep *.ipf -e "LoadWave"

This now gives us the lines containing LoadWave from within the code files.

...
ParseTimestampsFromOME.ipf:		LoadWave/O/Q/P=$pathName/J/N=OMEDumpWave/K=2 fileName
Storm54.ipf:	LoadWave/A=coord/J/K=1/L={0,1,0,0,0}/O
LoadColorTableCSVs.ipf:		LoadWave/Q/A=rgb/J/D/W/O/L={0,1,0,1,3}/P=expDiskFolder ThisFile
KinetochoreColoc.ipf:		LoadWave/M/O/J/K=1/L={0,1,0,0,0}/P=csvDiskFolder/A=$wName/Q fileName
...

and there the answer was…

That’s it! Other tips can be found with the tag tips from the blog.

The post title comes from “Find The Answer Within” by The Boo Radleys from their Wake Up! album