[英]How to use Unix commands in python that works on windows operating system
[英]Is there any way to use UNIX GREP commands on WIndows operating system
我需要一些命令,比如UNIX中的GREP for WINDOWS操作系統,如果有辦法在Windows中使用grep或任何等效的命令?
一些選擇:
如果你想要整套UNIX工具(編譯器,X服務器, 一切 ),Cygwin是很好的。 Cygwin DLL實際上為您提供了完整的UNIX環境,命令行工具,GUI內容和編程環境,因此您可以輕松地將UNIX軟件移植到Windows。
我自己更喜歡GnuWin32 (GNU for Windows),因為我可以添加我需要的東西。 它包含在包中,因此您無需安裝該批次。 它們也是為Windows本地編譯的,不需要Cygwin DLL。
例如,我最近有一種情況,我只需要在特定的盒子上使用awk來進行快速和臟的文本文件操作。 我不得不安裝整個Cygwin環境而且GnuWin32更容易(也更快)。
就grep
,您可能也有興趣知道Windows findstr
比以前的日子更強大 - 它現在可以做正則表達式。 它仍然不是我心愛的UNIX工具鏈后端的疙瘩:-)但它可能是一個可行的選擇,取決於您的需求:
C:\Documents and Settings\Pax> 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 occurances 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.
安裝Cygwin 。 它是一個unix模擬器,可以讓你在windows上使用正則表達式。
這個軟件過去對我很有用: 伯克利公用事業公司
如果你在窗戶上沒有grep,請不要擔心。 你可以使用像其中一個提到的findstr,或者只是做一些vbscript編程。
搜索模式的示例:(相當於grep“word”文件)
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile= "C:\test\file"
Set objFile = objFS.OpenTextFile(strFile,1)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine,"word")> 0 then
Wscript.Echo "Found word: " & strLine
End If
Loop
當然,如果您能夠負擔得起將資源下載到您的環境中的奢侈品,請使用GNU grep(和其他人)。 它們可以使您的批處理腳本體驗更輕松。
您可以嘗試使用Cygwin ,它是unix系統中常見的命令行工具的完整集合。
MSYS是另一個(輕量級)選項。
您可以嘗試在Windows上安裝chocolatey ,然后安裝Gow工具。
這將為您提供Windows上的grep
Gow
代表Windows上的GNU。 它在windows
上提供了unix command line utilities
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.