Just good advice? = WAS("Re: getting system date")

Bengt Richter bokr at oz.net
Wed May 14 17:48:55 EDT 2003


On Wed, 14 May 2003 10:00:34 GMT, Alex Martelli <aleax at aleax.it> wrote:
[...]
>
>grep on windows?  with cygwin or an add-on I guess;-).  But yes, the
<[OT]>
Real grep has to be an addon, but much of the basic capability can be
had via the not-very-adverstised

96-10-13  18:38     25,360 c:\winnt\system32\findstr.exe

which has been around a while. I believe it came on my NT4 disks (but I
can't be 100% certain, since I've installed a load of other stuff since).
There is also a related qgrep in an SDK. Neither are the real thing, but
they are a step up from

96-10-13  18:38     30,480 C:\WINNT\system32\find.exe

Of course, it's a pain switching syntax if you are used to python's re.

The "Find in Files" that's built in to the MSVC++ IDE is pretty nice, if
you don't mind the GUI input. It does regexes too (but the syntax with
too much escaping), and it eases navigation to what's found. I.e., you
can click and open the relevant doc and go to the line in context. That's _really_ handy.

--
[14:24] C:\pywk\grammar>findstr/?
Searches for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/F:file]
        [/C:string] [/G:file] [strings] [[drive:][path]filename[ ...]]

  /B        Matches pattern if at the beginning of a line.
  /E        Matches pattern if at the end of a line.
  /L        Uses search strings literally.
  /R        Uses search strings as regular expressions.
  /S        Searches for matching files in the current directory and all
            subdirectories.
  /I        Specifies that the search is not to be case-sensitive.
  /X        Prints lines that match exactly.
  /V        Prints only lines that do not contain a match.
  /N        Prints the line number before each line that matches.
  /M        Prints only the filename if a file contains a match.
  /O        Prints character offset before each matching line.
  /P        Skip files with non-printable characters
  /F:file   Reads file list from the specified file(/ stands for console).
  /C:string Uses specified string as a literal search string.
  /G:file   Gets search strings from the specified file(/ stands for console).
  strings   Text to be searched for.
  [drive:][path]filename
            Specifies a file or files to search.

Use spaces to separate multiple search strings unless the argument is prefixed
with /C.  For example, 'FINDSTR "hello there" x.y' searches for "hello" or
"there" in file x.y.  'FINDSTR /C:"hello there" x.y' searches for
"hello there" in file x.y.

For information on FINDSTR regular expressions refer to the online Command
Reference.
--

Of course, it's easy to build special purpose greppers with python and the re module. E.g.,

[14:25] C:\pywk\grammar>ff (?i)martelli.*bridge

    *** File e:\info\ddirs\ALLDIRFS  03-05-13 21:45:58 ***
03-04-12  18:08     10,498 C:\pywk\a_info\martelli-bridge.txt

(I periodically [I did it last night at 21:45:58 apparently] make a raw ascii directory
listing[1] of all the partitions on my disk in the format of that line, and ff "greps"
that file by default using sys.argv[1] as the re pattern).

[1] A cmd file does each partition in turn like

@echo CDIRF...
@dir/a  C:\/s | perl c:\util\dirf.pl . > e:\info\ddirs\ALLDIRFS
@echo DDIRF...
@dir/a  D:\/s | perl c:\util\dirf.pl . >> e:\info\ddirs\ALLDIRFS

Oops, THAT filter was before python for me ;-)

Anyway, BTW, I much prefer a concise filtered dir listing. E.g., compare
--
[14:29] C:\pywk>dir *bridge*/s
 Volume in drive C is System
 Volume Serial Number is 14CF-C4B9


 Directory of C:\pywk\a_info

03-04-12  18:08                 10,498 martelli-bridge.txt
               1 File(s)         10,498 bytes

     Total Files Listed:
               1 File(s)         10,498 bytes
                            130,656,768 bytes free
--
vs

[14:29] C:\pywk>dir *bridge*/s |perl c:\util\dirf.pl .
03-04-12  18:08     10,498 C:\pywk\a_info\martelli-bridge.txt

or more conveniently,

[14:30] C:\pywk>dirf . *bridge*/s
03-04-12  18:08     10,498 C:\pywk\a_info\martelli-bridge.txt

Note that having a full dir line in the ff data lets you use the regex
to filter on dates too. E.g., what archive files did I download or make in March?

[14:33] C:\pywk>ff "03-03-.*(zip|z)$"

    *** File e:\info\ddirs\ALLDIRFS  03-05-13 21:45:58 ***
03-03-12  10:22     20,058 C:\pywk\cwblock\cwblock.tar.gz
03-03-16  14:43     81,686 E:\DownLoad\Python\pysqlite-0_4_1_tar.gz
03-03-17  17:13      2,424 E:\Info\DataBases\csv.zip

I like this better than /usr/bin/locate, BTW.
</[OT]>

Regards,
Bengt Richter




More information about the Python-list mailing list