grep、awk 和 awk 之间有什么区别? sed?
我对 grep、awk 和 sed 在 Unix/Linux 系统管理和文本处理中的作用的差异感到困惑。
I am confused about the differences between grep
, awk
and sed
in terms of their role in Unix/Linux system administration and text processing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短定义:
grep
:搜索文件中的特定术语现在,
awk
和sed
与grep
完全不同。awk
和sed
是文本处理器。他们不仅能够在文本中找到您要查找的内容,还能够删除、添加和修改文本(以及更多)。awk
主要用于数据提取和报告。sed
是一个流编辑器它们中的每一种都有其自己的功能和特点。
示例
Sed
Awk
基本
awk
用法:计算总和/平均值/最大/最小/等。
我建议您阅读这本书:Sed & Awk:第 2 版。
它将帮助您在任何类 UNIX 环境中成为熟练的 sed/awk 用户。
Short definition:
grep
: search for specific terms in a fileNow
awk
andsed
are completly different thangrep
.awk
andsed
are text processors. Not only do they have the ability to find what you are looking for in text, they have the ability to remove, add and modify the text as well (and much more).awk
is mostly used for data extraction and reporting.sed
is a stream editorEach one of them has its own functionality and specialties.
Example
Sed
Awk
Basic
awk
usage:Compute sum/average/max/min/etc. what ever you may need.
I recommend that you read this book: Sed & Awk: 2nd Ed.
It will help you become a proficient sed/awk user on any unix-like environment.
如果您想快速搜索文件中匹配的行,Grep 非常有用。它还可以返回一些其他简单信息,例如匹配行号、匹配计数和文件名列表。
Awk 是一种完整的编程语言,围绕读取 CSV 样式文件、处理记录以及可选地打印结果数据集而构建。它可以做很多事情,但它并不是完成简单任务的最简单的工具。
当您想要基于正则表达式更改文件时,Sed 非常有用。它允许您轻松匹配部分线条、进行修改并打印结果。它的表达能力不如 awk,但这使它更容易用于简单的任务。它有许多更复杂的运算符可以使用(我认为它甚至是图灵完备的),但一般来说你不会使用这些功能。
Grep is useful if you want to quickly search for lines that match in a file. It can also return some other simple information like matching line numbers, match count, and file name lists.
Awk is an entire programming language built around reading CSV-style files, processing the records, and optionally printing out a result data set. It can do many things but it is not the easiest tool to use for simple tasks.
Sed is useful when you want to make changes to a file based on regular expressions. It allows you to easily match parts of lines, make modifications, and print out results. It's less expressive than awk but that lends it to somewhat easier use for simple tasks. It has many more complicated operators you can use (I think it's even turing complete), but in general you won't use those features.
我只想提一件事,有很多工具可以进行文本处理,例如
sort、cut、split、join、paste、comm、uniq、column、rev、tac、tr、nl、pr、head、tail.....
它们非常方便,但你必须学习它们的选项等。
一种懒惰的方法(不是最好的方法)学习文本处理可能是:只学习 grep 、 sed 和 awk 。使用这三个工具,您可以解决几乎 99% 的文本处理问题,并且不需要记住上述不同的命令和选项。 :)
而且,如果您已经学习并使用过这三个,您就会知道其中的区别。实际上,这里的区别是指哪种工具擅长解决什么样的问题。
一种更懒惰的方法可能是学习脚本语言(python、perl 或 ruby)并用它进行所有文本处理。
I just want to mention a thing, there are many tools can do text processing, e.g.
sort, cut, split, join, paste, comm, uniq, column, rev, tac, tr, nl, pr, head, tail.....
they are very handy but you have to learn their options etc.
A lazy way (not the best way) to learn text processing might be: only learn grep , sed and awk. with this three tools, you can solve almost 99% of text processing problems and don't need to memorize above different cmds and options. :)
AND, if you 've learned and used the three, you knew the difference. Actually, the difference here means which tool is good at solving what kind of problem.
a more lazy way might be learning a script language (python, perl or ruby) and do every text processing with it.