sed、grep、find、awk、gawk 之间有什么关系,首选哪个?
我是 Linux/Unix 新手。我发现使用这些命令可以通过多种方式完成某些任务。他们之间有什么关系吗?或者说更喜欢哪一个呢?哪一个已经过时了?
谢谢。
I'm new to Linux/Unix. I find some task can be accomplished by many ways using these commands. Is there some relationship between them? Or which one is preferred? Which one is outdated?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请参阅 SO 366980 了解有关Perl、Python、Awk 和 Sed 之间的区别。
五个命令列表中有四个不同的命令; awk 和 gawk 密切相关,GNU Awk 是 Awk 的 GNU 实现。
find
用于根据文件特征(例如名称或修改时间)在一组目录中查找文件。 GNU Find 比传统或 POSIX 版本的 Find 具有更多的功能。grep
用于使用正则表达式来定位文件中的内容来控制选择的内容。 GNU Grep 比传统的或 POSIX 版本的 Grep 具有更多的功能。sed
用于使用编辑命令(包括正则表达式)修改文件内容。 GNU Sed 比传统或 POSIX 版本的 Sed 具有更多功能。awk
是一种模式匹配和格式化语言。它是一种编程语言,而您提到的其他工具则不是。当需要的时候,它非常有用。然而,Perl 和 Python 也具有 Awk 的功能和许多附加功能,因此很多人使用它们来代替 Awk。 GNU Awk 比传统或 POSIX 版本的 Awk 具有更多功能。因此,您列出的工具可以完成不同的工作,但可以通过多种方式协同工作。您应该了解的另一个工具是 xargs,它获取文件列表并依次对每个文件运行指定的命令。
See SO 366980 for discussion about the differences between Perl, Python, Awk and Sed.
There are four distinct commands in the list of five; awk and gawk are closely related, with GNU Awk being the GNU implementation of Awk.
find
is for locating files in a set of directories based on file characteristics such as name or modification time. GNU Find has many more capabilities than the traditional or POSIX versions of Find.grep
is for locating content within files using regular expressions to control what is selected. GNU Grep has many more capabilities than the traditional or POSIX versions of Grep.sed
is for modifying the contents of files using editing commands, including regular expressions. The GNU Sed has many more capabilities than the traditional or POSIX versions of Sed.awk
is a pattern matching and formatting language. It is a programming language in a way that the other tools you mention are not. When needed, it is very useful. However, Perl and Python also have the capabilities of Awk and many extras, so many people use them instead of Awk. GNU Awk has some more capabilities than the traditional or POSIX versions of Awk.So, the tools you list do different jobs, but can work together in many ways. One other tool you should be aware of is
xargs
, which takes lists of files and runs a specified command on each of the files in turn.AWK 是由 Aho、Weinberger 和 Kernighan 设计的一种编程语言。
gawk
是 AWK 的一种实现,但还有其他几种实现,包括mawk
和nawk
。它是一种成熟的编程语言,具有变量、控制结构和关联数组,但通常针对处理 UNIX 系统上常见的基于文本的数据进行了优化。sed
是流编辑器,受到ed
编辑器的启发。它有一个简单的命令集,主要限于逐行编辑。sed
命令可以很容易地在awk
中模仿。以下是等效的:grep
查找文本。基本的grep
功能可以在sed
和awk
中轻松模仿。以下是等效的:哎呀,错过了
find
。find
遍历文件系统树,根据指定的标准执行操作。例如,将从当前目录
.
开始遍历所有文件和目录,排除(且不下降到)名称以.
开头的任何目录,并运行cat
在名称不以~
结尾的每个文件上(结果是打印出该文件的内容)。同样,这在 AWK 或 Perl 或许多其他编程语言(甚至在本例中是纯 shell)中都是可行的,但使用专用工具更容易、更快地编写和理解。AWK is a programming language designed by Aho, Weinberger, and Kernighan.
gawk
is one implementation of AWK, but there are several others, includingmawk
, andnawk
. It is a full-blown programming language with variables, control structures, and associative arrays, but generally optimized for dealing with the sorts of text-based data commonly found on UNIX systems.sed
is the stream editor, inspired byed
the editor. It has a simple command set mostly limited to line-by-line editing.sed
commands can easily by imitated inawk
. The following are equivalent:grep
finds text. Basicgrep
functionality can easily be imitated insed
andawk
. The following are equivalent:Oops, missed
find
.find
walks the filesystem tree, performing actions according to specified criterion. For example,will walk all files and directories starting from the current directory
.
, excluding (and not descending into) any directories with names starting with.
, and runcat
on every file whose name does not end with~
(with the result of printing out the contents of that file). Again, this would be doable in AWK or Perl or many other programming languages (or even pure shell, in this example), but is easier and faster to write and understand with a special-purpose tool.不同的工具适合不同的工作。问题就像“什么是更好的工具:锤子、螺丝刀还是组合钳?”
grep - 在文件/流中查找/过滤文本。行匹配/不匹配 - 将其流式传输到输出。用于将输出量减少到您需要的量。如果您获得太多信息,通常以交互方式使用。也经常在脚本中用于拉出“包含我需要的内容的一行”
通常在脚本化使用 grep 之后,您会看到 awk (复杂的表达式或只是一个老派程序员)或 cut (速度更快但相当简单)提取一个值。 (grep 水平切,cut/awk 垂直切)
使用 awk 进行全功能编程目前几乎见不到。
sed - 搜索和替换,通常是脚本化的(如果您想以交互方式进行操作,可视化编辑器会更好 - 您会看到正在执行的操作。)
find - 查找符合给定(高级)参数的文件。
Different tools for different jobs. The question is like "what is a better tool: a hammer, a screwdriver or combination pliers?"
grep - find/filter text in a file/stream. Line matches/doesn't match - stream it to output. Use to reduce amount of output to what you need. Commonly used interactively if you get too much information. Also often used in scripts to pull "that one line containing what I need"
Usually after scripted use of grep you see either awk (either a complicated expression or just an old-school programmer) or cut (which is much faster but pretty simplistic) to extract the one value. (grep chops horizontally, cut/awk chops vertically)
Using awk for fully-featured programming is currently almost unseen.
sed - search-and-replace, usually scripted (a visual editor is much better for that if you want to do it interactively - you see what is being done.)
find - find files conforming to given (advanced) parameters.
这些工具执行不同的操作,因此首选哪一个取决于您想要做什么。
在您提到的工具中,
find
与其他工具略有不同 - 它在文件系统上搜索具有某些属性(名称、日期、权限...)的文件。sed
和grep
将文本文件作为输入,对其进行操作,然后输出结果。他们都大量使用正则表达式。sed
(“Stream EEditor”)通常用于搜索和替换操作。grep
(“全局正则表达式打印”)在文本文件中输出与某些模式匹配的行。在 Linux 系统上,
awk
和gawk
通常是同一个程序的不同名称,即 AWK 的 GNU 版本(以其创建者“Aho、Weinberger、Kernighan”命名)。awk
也可以指没有 GNU 扩展的awk
的“经典”版本,并且还有其他变体nawk
(" new awk”)和mawk
(“迈克的 AWK”)。awk
的作用远不止于模式匹配;它确实是一种成熟的编程语言,尽管它旨在将输入文本分解为行和字段并对其进行操作(包括数字操作,因此很容易执行诸如总计列或进行统计计算之类的操作)。These tools do different things, so which one is preferred depends on what you want to do.
Of the tools you mention,
find
is a little different than the others -- it works on filesystems to search for files with certain attributes (name, date, permissions...).sed
andgrep
take text files as input, operate on them, and output the results. They both heavily use regular expressions.sed
("Stream EDitor") is generally used for search-and-replace operations.grep
("Global Regular Expression Print") outputs lines in a text file that match some pattern.On Linux systems,
awk
andgawk
are generally different names for the same program, the GNU version of AWK (named after its creators "Aho, Weinberger, Kernighan").awk
can also refer to the "classic" version ofawk
that doesn't have the GNU extensions, and there are other variantsnawk
("new awk") andmawk
("Mike's AWK").awk
does far more than pattern matching; it's really a full-blown programming language, though it's geared toward breaking up input text into lines and fields and operating on them (including numeric operations, so it's easy to do things like total up columns or do statistical calculations).按我使用它们的频率顺序列出,并附有评论。有关更多详细信息,请在命令行尝试
man 命令
,例如man grep
以获取更多详细信息。grep - 打印与模式匹配的行 * 我个人认为这是最有用的。
find - 遍历文件层次结构 * 可用于查找子目录中的文件或
在文件上执行 shell 命令
sed - 流编辑器 * 帮助自动对批量文件进行简单的文本编辑
awk - 模式定向扫描和处理语言 * 很长时间没有使用它了。
如果需要使用 awk,我通常会使用 Ruby 之类的东西。
Listed in order of how often I use them, appended with comment. For more details, try
man command
, egman grep
at the command line for more details.grep - print lines matching a pattern * I personally find this the most useful.
find - walk a file hierarchy * can be used to find files in subdirectories or
execute shell commands on files
sed - stream editor * helps automate simple text edits on batches of files
awk - pattern-directed scanning and processing language * haven't used it for a long time.
If it gets to the point I need to use awk, I generally use something like Ruby.