บรรทัดคำสั่ง grep แบบเรียกซ้ำของ Windows


205

ฉันต้องทำ grep แบบเรียกซ้ำใน Windows สิ่งนี้ใน Unix / Linux:

grep -i 'string' `find . -print`

หรือวิธีที่ต้องการมากขึ้น:

find . -print | xargs grep -i 'string'

ฉันติดแค่ cmd.exe ดังนั้นฉันจึงมีเฉพาะคำสั่งในตัวของ Windows ฉันไม่สามารถติดตั้งCygwinหรือเครื่องมือของบุคคลที่สามเช่นUnxUtilsบนเซิร์ฟเวอร์นี้ได้อย่างน่าเสียดาย ฉันไม่แน่ใจด้วยซ้ำว่าฉันสามารถติดตั้ง PowerShell ได้ คำแนะนำใด ๆ ที่ใช้ built-in cmd.exe (Windows 2003 Server)


นั่นเป็นเรื่องที่ยากหากไม่มี PowerShell ทำไมคุณถึงติดตั้งไม่ได้
Chris Ballance

ผู้ดูแลระบบ sys กำลังล็อคสิทธิ์บนเซิร์ฟเวอร์ของเรา หากใครมีคำแนะนำ powershell ทิ้งพวกเขาและฉันจะดูว่าเราสามารถติดตั้ง PowerShell
Andy White

3
btw ฉันพบว่าใน linux จะดีกว่าที่จะเขียน: "find. | xargs grep -i string" ความแตกต่างคือถ้าการค้นหาส่งคืนรายการที่ยาวมากคุณอาจเกินความยาวคำสั่งสูงสุด (มันเกิดขึ้นกับฉัน) และคุณจะไม่สามารถ grep ได้เลย ด้วย xargs grep เรียกว่าหนึ่งครั้งต่อไฟล์ที่พบ
Nathan Fellman

Grep หลายรุ่นรวมถึง Gnu Grep เสนอการค้นหาแบบเรียกซ้ำ ( gnu.org/software/grep/manual/ ...... ) เพื่อให้การค้นหาของคุณสามารถเขียนgrep -i 'string' -R .ได้เช่น @NathanFellman แนะนำหลีกเลี่ยงปัญหาของคำสั่งที่ยาวเกินไป
Scott Centoni

คำตอบ:


252

findstr สามารถทำการค้นหาซ้ำ (/ S) และสนับสนุนตัวแปรของ regex ไวยากรณ์ (/ R) บางอย่าง

C:\>findstr /?
Searches for strings in files.

FINDSTR [/B] [/E] [/L] [/R] [/S] [/I] [/X] [/V] [/N] [/M] [/O] [/P] [/F:file]
        [/C:string] [/G:file] [/D:dir list] [/A:color attributes] [/OFF[LINE]]
        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.
  /OFF[LINE] Do not skip files with offline attribute set.
  /A:attr    Specifies color attribute with two hex digits. See "color /?"
  /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).
  /D:dir     Search a semicolon delimited list of directories
  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.

Regular expression quick reference:
  .        Wildcard: any character
  *        Repeat: zero or more occurrences of previous character or class
  ^        Line position: beginning of line
  $        Line position: end of line
  [class]  Character class: any one character in set
  [^class] Inverse class: any one character not in set
  [x-y]    Range: any characters within the specified range
  \x       Escape: literal use of metacharacter x
  \<xyz    Word position: beginning of word
  xyz\>    Word position: end of word

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

45
findstr เป็นการแทนที่ที่ดีสำหรับ grep ฉันมักจะใช้ findstr / sinp (recursive กรณีตายข้ามไฟล์ไบนารีและตัวเลขแสดงบรรทัด)
สตีฟโรว์

5
ขออภัย findstr มีการสนับสนุนที่ จำกัด มากสำหรับ regex ตามเอกสารและรูปแบบที่ฉันพยายามใช้
John Kaster

3
ถอนหายใจวางใจ Microsoft ให้เพิ่มยูทิลิตี้ใหม่ (findstr) แทนการแก้ไขอันที่มีอยู่ (find) หากคุณต้องการ findstr สามารถนับจำนวนบรรทัดได้ให้ใช้ - findstr [options] | find / c / v "" - ใช้ findstr เพื่อจับคู่บรรทัดและค้นหาเพื่อนับ ใช่พบว่าไม่มีบรรทัดที่ตรงกับสตริงว่างดังนั้นด้วย / v ทุกบรรทัดจะจับคู่
yoyo

3
พวกเขาไม่ต้องการทำลายท่อที่มีอยู่ซึ่งพึ่งพาพฤติกรรมที่ไม่เหมาะสมของ find
i_am_jorf

134
findstr /spin /c:"string" [files]

พารามิเตอร์มีความหมายต่อไปนี้:

  • s = เรียกซ้ำ
  • p = ข้ามอักขระที่ไม่สามารถพิมพ์ได้
  • i = ตัวพิมพ์เล็กและตัวพิมพ์ใหญ่
  • n = พิมพ์หมายเลขบรรทัด

และสตริงที่ต้องการค้นหาคือบิตที่คุณใส่ในเครื่องหมายคำพูดหลังจากนั้น /c:


2
ขอโทษ คุณสามารถเพิ่มตัวอย่างได้หรือไม่ อะไรนะspin? มันเป็นบรรทัดของข้อความที่จะหา? และไม่ใช้ / g หรือ / f เพื่อระบุไฟล์? แล้วเครื่องหมายวงเล็บเหลี่ยมคืออะไร?
Wolfpack'08

5
findstr /?อธิบายแต่ละพารามิเตอร์ s = เรียกซ้ำ, p = ข้ามอักขระที่ไม่สามารถพิมพ์ได้, i = ตัวพิมพ์เล็กและตัวพิมพ์ใหญ่, n = หมายเลขบรรทัดพิมพ์ คุณไม่จำเป็นต้องใช้สิ่งเหล่านี้ทั้งหมด แต่ฉันชอบพวกมันและspinจดจำได้ง่าย /c:สตริงการค้นหาเป็นบิตที่คุณใส่ในเครื่องหมายคำพูดหลัง
i_am_jorf

3
โอ้ฮ่าฮ่า ผมแต่ที่จริงผมไม่ทราบว่าการปรับเปลี่ยนมาใช้เช่น/? /spinฉันคิดว่ามันถูกใช้เหมือน/s/p/i/nกัน
Wolfpack'08

4
ใช่โดยทั่วไป โปรแกรม cmd บางโปรแกรมช่วยให้คุณลดการสั่นไหว/ได้ นี่คือหนึ่ง ไม่ใช่ทุกคนที่ให้คุณทำอย่างนั้น คุณรู้ไหมว่าคำสั่งพิเศษมาก
i_am_jorf

2
อย่างไรก็ตามมีการแจกจ่ายเป็นส่วนหนึ่งของ Windows ดังนั้นจึงตรงตามข้อกำหนดดั้งเดิมของสิ่งที่สามารถทำได้ด้วย cmd.exe ที่ไม่ต้องการซอฟต์แวร์เพิ่มเติมเพื่อติดตั้ง
i_am_jorf

26

ฉันค้นหาข้อความด้วยคำสั่งต่อไปนี้ซึ่งระบุชื่อไฟล์ทั้งหมดที่มี 'ข้อความค้นหา' ที่ระบุ

C:\Users\ak47\Desktop\trunk>findstr /S /I /M /C:"search text" *.*

13

ฉันขอแนะนำเครื่องมือที่ยอดเยี่ยมจริงๆ:

utils พื้นเมือง

เพียงแค่แกะมันออกแล้วใส่โฟลเดอร์นั้นลงในตัวแปรสภาพแวดล้อม PATH และ voila! :)

ทำงานเหมือนจับใจและมีอีกมากมายแล้ว grep;)


5
@mPrinC คำถามบอกว่า "ฉันไม่สามารถติดตั้ง Cygwin หรือเครื่องมือของบุคคลที่สามเช่น UnxUtils บนเซิร์ฟเวอร์นี้อย่างน่าเสียดาย"
martin jakubik

7
โหวตขึ้นเพราะมันยังมีประโยชน์กับฉัน นอกจากนี้มันไม่จำเป็นต้องมี 'การติดตั้ง' เพียงดึงมันไว้ที่ไหนสักแห่ง
Rosdi Kasim


9
for /f %G in ('dir *.cpp *.h /s/b') do  ( find /i "what you search"  "%G") >> out_file.txt

นี่ดูเหมือนกับคำตอบนี้ที่นี่serverfault.com/a/506615แต่ฉันชอบความจริงที่ว่าคุณตอบคำตอบไปยังไฟล์ บริโภคง่ายกว่ามาก
jinglesthula

สำหรับการอ้างอิงด้วย findstr คุณจะได้รับไฮไลต์ชื่อไฟล์ในบรรทัดคำสั่งและคุณจะไม่ได้รับสิ่งนี้ในไฟล์ข้อความ (ชัด) ดังนั้นจึงไม่ใช่ไฟล์ที่ระบุว่าเป็นรูปแบบที่มีประโยชน์มากกว่า
Martin Greenaway

5

Select-Stringทำงานได้ดีที่สุดสำหรับฉัน ตัวเลือกอื่น ๆ ทั้งหมดที่ระบุไว้ที่นี่เช่นfindstrไม่ทำงานกับไฟล์ขนาดใหญ่

นี่คือตัวอย่าง:

select-string -pattern "<pattern>" -path "<path>"

หมายเหตุ : สิ่งนี้ต้องใช้ Powershell


โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.