grep 是什么?grep 是什么?grep 是什么?grep 是什么?
  • 文章
  • 正则表达式
    • 工具
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容
发表 admin at 2024年3月5日
类别
  • 正则表达式
标签
grep 是什么?
  • 简
  • 繁
  • En
关于正则表达式 » 正则表达式工具和实用程序 » grep 是什么?

正则表达式工具
grep
语言和函数库
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
数据库
MySQL
Oracle
PostgreSQL
本网站其他内容
简介
正则表达式快速入门
正则表达式教程
替换字符串教程
应用程序和语言
正则表达式范例
正则表达式参考
替换字符串参考

grep 是什么?

Grep 是一种工具,源自 1970 年代的 UNIX 世界。它可以在文件和文件夹(UNIX 中的目录)中搜索,并检查这些文件中的哪些列符合给定的 正则表达式。Grep 会输出文件名称和列号,或与正则表达式相符的实际列。总而言之,这是一个非常有用的工具,可用于在电脑上的任何地方寻找保存的信息,即使(或特别是)您不知道实际要在哪里寻找。

使用 grep

如果您输入 grep regex *.txt,grep 会搜索目前文件夹中的所有文本文件。它会将 regex 套用至文件中的每一列,并打印(即显示)找到相符项目的每一列。这表示 grep 本质上是基于列的。正则表达式相符项目无法跨越多列。

如果您喜欢在命令行上工作,传统的 grep 工具会让许多任务作变得更容易。所有 Linux 发行版(除了极小的软驱版本)都缺省安装 grep 的版本,通常是 GNU grep。如果您使用 Microsoft Windows,则需要另外下载并安装它。如果您使用 Borland 开发工具,则已经安装了 Borland 的 Turbo GREP。

grep 不仅适用于 globbed 文件,也适用于您在标准输入中提供的任何内容。与标准输入搭配使用时,grep 会打印从标准输入读取的所有符合 regex 的列。例如:Linux find 指令会对目前目录进行 glob,并打印找到的所有文件名称,因此 find | grep regex 只会打印与 regex 相符的文件名称。

Grep 的 Regex 引擎

大多数版本的 grep 使用 regex 指导的 引擎,例如本网站 regex 教程 中所讨论的 regex 风格。然而,grep 的 regex 风格非常有限。在 POSIX 系统上,它使用 POSIX 基本正则表达式。

grep 的加强版本称为 egrep。它使用 文本导向引擎。由于 grep 和 egrep 都不支持任何特殊功能,例如 延迟重复 或 环顾,而且因为 grep 和 egrep 只会指出是否在特定行上找到配对,因此这个区别并不重要,除了文本导向引擎比较快。在 POSIX 系统上,egrep 使用 POSIX 扩展正则表达式。尽管名称为「延伸」,但 egrep 几乎与 grep 相同。它只使用略有不同的 regex 语法,并添加对交替的支持,但失去对反向引用的支持。

GNU grep 是 Linux 上最受欢迎的 grep 版本,它同时使用文本导向和 regex 指导引擎。如果您使用反向引用,它会使用 regex 指导引擎。否则,它会使用较快的文本导向引擎。同样地,对于 grep 所设计的任务,这对您这个用户来说并不重要。如果您输入「grep」指令,您将使用 GNU 基本正则表达式语法。如果您输入「egrep」指令,您将使用 GNU 扩展正则表达式语法。GNU 版本的 grep 和 egrep 具有完全相同的功能,包括 grep 的交替和 egrep 的反向引用。它们只使用略有不同的语法。

超越指令列

如果您喜欢在命令行上工作,那么传统的 grep 工具就是您的选择。但如果您喜欢使用图形用户接口,则有许多类似的 grep 工具可供 Windows 和其他平台使用。只要在您最爱的软件下载网站上搜索「grep」即可。不幸的是,许多 grep 工具附带的说明文档很差,让您必须自行找出它们确切使用的正则表达式类型。并非宣称与 Perl 兼容就表示它们实际上兼容。有些几乎完全兼容(但从未完全相同),但当您想要使用高端且非常有用的建构(例如 环顾)时,其他工具则会惨不忍睹。

grep 是什麼?
  • 简
  • 繁
  • En
關於正規表示式 » 正規表示式工具和實用程式 » grep 是什麼?

正規表示式工具
grep
語言和函式庫
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery 和 XPath
XRegExp
資料庫
MySQL
Oracle
PostgreSQL
本網站其他內容
簡介
正規表示式快速入門
正規表示式教學
替換字串教學
應用程式和語言
正規表示式範例
正規表示式參考
替換字串參考

grep 是什麼?

Grep 是一種工具,源自 1970 年代的 UNIX 世界。它可以在檔案和資料夾(UNIX 中的目錄)中搜尋,並檢查這些檔案中的哪些列符合給定的 正規表示式。Grep 會輸出檔案名稱和列號,或與正規表示式相符的實際列。總而言之,這是一個非常有用的工具,可用於在電腦上的任何地方尋找儲存的資訊,即使(或特別是)您不知道實際要在哪裡尋找。

使用 grep

如果您輸入 grep regex *.txt,grep 會搜尋目前資料夾中的所有文字檔案。它會將 regex 套用至檔案中的每一列,並列印(即顯示)找到相符項目的每一列。這表示 grep 本質上是基於列的。正規表示式相符項目無法跨越多列。

如果您喜歡在命令列上工作,傳統的 grep 工具會讓許多工作變得更容易。所有 Linux 發行版(除了極小的軟碟機版本)都預設安裝 grep 的版本,通常是 GNU grep。如果您使用 Microsoft Windows,則需要另外下載並安裝它。如果您使用 Borland 開發工具,則已經安裝了 Borland 的 Turbo GREP。

grep 不僅適用於 globbed 檔案,也適用於您在標準輸入中提供的任何內容。與標準輸入搭配使用時,grep 會列印從標準輸入讀取的所有符合 regex 的列。例如:Linux find 指令會對目前目錄進行 glob,並列印找到的所有檔案名稱,因此 find | grep regex 只會列印與 regex 相符的檔案名稱。

Grep 的 Regex 引擎

大多數版本的 grep 使用 regex 指導的 引擎,例如本網站 regex 教學課程 中所討論的 regex 風格。然而,grep 的 regex 風格非常有限。在 POSIX 系統上,它使用 POSIX 基本正規表示式。

grep 的加強版本稱為 egrep。它使用 文字導向引擎。由於 grep 和 egrep 都不支援任何特殊功能,例如 延遲重複 或 環顧,而且因為 grep 和 egrep 只會指出是否在特定行上找到配對,因此這個區別並不重要,除了文字導向引擎比較快。在 POSIX 系統上,egrep 使用 POSIX 延伸正規表示式。儘管名稱為「延伸」,但 egrep 幾乎與 grep 相同。它只使用略有不同的 regex 語法,並新增對交替的支援,但失去對反向參照的支援。

GNU grep 是 Linux 上最受歡迎的 grep 版本,它同時使用文字導向和 regex 指導引擎。如果您使用反向參照,它會使用 regex 指導引擎。否則,它會使用較快的文字導向引擎。同樣地,對於 grep 所設計的任務,這對您這個使用者來說並不重要。如果您輸入「grep」指令,您將使用 GNU 基本正規表示式語法。如果您輸入「egrep」指令,您將使用 GNU 扩展正規表示式語法。GNU 版本的 grep 和 egrep 具有完全相同的功能,包括 grep 的交替和 egrep 的反向參照。它們只使用略有不同的語法。

超越指令列

如果您喜歡在命令列上工作,那麼傳統的 grep 工具就是您的選擇。但如果您喜歡使用圖形使用者介面,則有許多類似的 grep 工具可供 Windows 和其他平台使用。只要在您最愛的軟體下載網站上搜尋「grep」即可。不幸的是,許多 grep 工具附帶的說明文件很差,讓您必須自行找出它們確切使用的正規表示式類型。並非宣稱與 Perl 相容就表示它們實際上相容。有些幾乎完全相容(但從未完全相同),但當您想要使用進階且非常有用的建構(例如 環顧)時,其他工具則會慘不忍睹。

What Is grep?
  • 简
  • 繁
  • En
About Regular Expressions » Tools and Utilities for Regular Expressions » What Is grep?

Regex Tools
grep
Languages & Libraries
Boost
Delphi
GNU (Linux)
Groovy
Java
JavaScript
.NET
PCRE (C/C++)
PCRE2 (C/C++)
Perl
PHP
POSIX
PowerShell
Python
R
Ruby
std::regex
Tcl
VBScript
Visual Basic 6
wxWidgets
XML Schema
Xojo
XQuery & XPath
XRegExp
Databases
MySQL
Oracle
PostgreSQL
More on This Site
Introduction
Regular Expressions Quick Start
Regular Expressions Tutorial
Replacement Strings Tutorial
Applications and Languages
Regular Expressions Examples
Regular Expressions Reference
Replacement Strings Reference

What Is grep?

Grep is a tool that originated from the UNIX world during the 1970’s. It can search through files and folders (directories in UNIX) and check which lines in those files match a given regular expression. Grep will output the filenames and the line numbers or the actual lines that matched the regular expression. All in all a very useful tool for locating information stored anywhere on your computer, even (or especially) if you do not really know where to look.

Using grep

If you type grep regex *.txt grep will search through all text files in the current folder. It will apply the regex to each line in the files, and print (i.e. display) each line on which a match was found. This means that grep is inherently line-based. Regex matches cannot span multiple lines.

If you like to work on the command line, the traditional grep tool will make a lot of tasks easier. All Linux distributions (except tiny floppy-based ones) install a version of grep by default, usually GNU grep. If you are using Microsoft Windows, you will need to download and install it separately. If you use Borland development tools, you already have Borland’s Turbo GREP installed.

grep not only works with globbed files, but also with anything you supply on the standard input. When used with standard input, grep will print all lines it reads from standard input that match the regex. E.g.: the Linux find command will glob the current directory and print all file names it finds, so find | grep regex will print only the file names that match regex.

Grep’s Regex Engine

Most versions of grep use a regex-directed engine, like the regex flavors discussed in the regex tutorial on this website . However, grep’s regex flavor is very limited. On POSIX systems, it uses POSIX Basic Regular Expressions.

An enhanced version of grep is called egrep. It uses a text-directed engine. Since neither grep nor egrep support any of the special features such as lazy repetition or lookaround, and because grep and egrep only indicate whether a match was found on a particular line or not, this distinction does not matter, except that the text-directed engine is faster. On POSIX systems, egrep uses POSIX Extended Regular Expressions. Despite the name “extended”, egrep is almost the same as grep. It just uses a slightly different regex syntax and adds support for alternation, but loses support for backreferences.

GNU grep, the most popular version of grep on Linux, uses both a text-directed and a regex-directed engine. If you use backreferences it uses the regex-directed engine. Otherwise, it uses the faster text-directed engine. Again, for the tasks that grep is designed for, this does not matter to you, the user. If you type the “grep” command, you’ll use the GNU Basic Regular Expressions syntax. If you type the “egrep” command, you’ll use the GNU Extended Regular Expressions syntax. The GNU versions of grep and egrep have exactly the same capabilities, including alternation for grep and backreferences for egrep. They only use a slightly different syntax.

Beyond The Command Line

If you like to work on the command line, then the traditional grep tool is for you. But if you like to use a graphical user interface, there are many grep-like tools available for Windows and other platforms. Simply search for “grep” on your favorite software download site. Unfortunately, many grep tools come with poor documentation, leaving it up to you to figure out exactly which regex flavor they use. It’s not because they claim to be Perl-compatible, that they actually are. Some are almost perfectly compatible (but never identical, though), but others fail miserably when you want to use advanced and very useful constructs like lookaround.

©2015-2025 艾丽卡 support@alaica.com