Announcement

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

  • Search within do-files on Mac OS

    Dear community,

    I wish to search within do-files on my Macbook. In particular, to search for commands/snippets I have used before in other files with the usual Spotlight search in the Finder.

    This does not seem to work by default (Stata 18, Macbook Pro M3 2023, OS 14.4.1).

    I have no recall if this ever was different on Mac but was wondering if someone knows more about this.

    Surprisingly, I didn't find any threads on this on Statalist.

    Best,

    John

  • #2
    Searching for text snippets in do-files is also a frustrating task on Windows. Here's a within Stata method that is not OS dependent.

    Code:
    // NAVIGATE TO THE DIRECTORY YOU WISH TO SEARCH IN
    // THIS CODE WILL ALSO SEARCH ANY SUBDIRECTORIES OF THAT
    clear
    filelist, pattern(*.do)
    gen strL contents = fileread(dirname+"/"+filename)
    gen double foundit = strpos(contents, string_you_are_searching_for)
    list dirname filename if foundit
    If you are searching for a family of strings that can be defined with wildcard notation, you can use the -strmatch()- function instead of -strpos()-. For even greater flexibility, you can use the -ustrregexm()- function to identify the presence of a family of strings defined by regular expressions. Note that the -double- in the -gen double foundit...- command is only needed if there are files to be searched that are so long that the number of characters exceeds what can be stored in a float. That is very unusual, at least for do-files that I write, and I don't normally include -double- there.

    Also, if you do not want to search the subdirectories, -filelist- allows a -norecursive- option. Do see -help filelist-.*

    I use this code frequently in my workflow.

    *-filelist- is written by Robert Picard and is available from SSC.

    Comment


    • #3
      I don't think it's possible in Spotlight, but you can pull up a folder in Finder, then press ⌘F to bring up the Search options, select "Name" > "ends with" and put in ".do", then click "+" to add another condition, then "Contents", and put in the string you want to search for.

      Comment


      • #4
        Would it be possible to use -grep- on terminal?

        Code:
         
         grep -R 'string' .
        Do not forget the . to allow to search recursively within the directory.

        Comment

        Working...
        X