Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Search for line of code

    I cant locate one of my do-files anymore. Is it possible to search windows by line of code in the Stata file?

  • #2
    Here's how I do it. Suppose I'm trying to find a Stata do file that I know contains some specific text "my search text"
    Code:
    filelist, pattern(*.do)
    gen strL contents = fileread(dirname+"/"+filename)
    gen long foundit = strpos(contents, "my search text")
    list dirname filename if foundit > 0
    Of course, you have to know exactly what text you're looking for. If you only know it vaguely, you might be able to use regular expressions instead of -strpos()- to try to match it. Also, if you have more than one file with that text, this will find them all, and then you may have to do some secondary searches to narrow it down. For example you might -keep if foundit > 0- and then -gen long foundit2 = strpos(contents, "some other text it should have")- and see what turns up that way.

    Added: -filelist- is by Robert Picard and is available from SSC.

    Comment


    • #3
      Awesome, thanks

      Comment


      • #4
        This is something I much prefer to use *nix command line utilities for, specifically -grep- which will also facilitate regular expression based searches. On windows it usually means you need Cygwin or some other Unix subsystem for Windows, so it’s definitely not a generic solution.

        Comment


        • #5
          #2 is clever, but I tend to agree with the sentiment in #4. Cygwin is an excellent tool for windows if you already know the Linux command line. I like it for SSH as well (over putty), though I believe windows 11 now supports SSH out of the box. I tried to learn Windows PowerShell once upon a time, but never got to a place where I was comfortable enough with it that I didn't find it frustrating to use.

          Comment

          Working...
          X