grep、awk 和 awk 之间有什么区别? sed?

发布于 2024-12-09 18:57:22 字数 59 浏览 0 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诠释孤独 2024-12-16 18:57:22

简短定义:

grep:搜索文件中的特定术语

#usage
$ grep This file.txt
Every line containing "This"
Every line containing "This"
Every line containing "This"
Every line containing "This"

$ cat file.txt
Every line containing "This"
Every line containing "This"
Every line containing "That"
Every line containing "This"
Every line containing "This"

现在,awksedgrep 完全不同。
awksed 是文本处理器。他们不仅能够在文本中找到您要查找的内容,还能够删除、添加和修改文本(以及更多)。

awk 主要用于数据提取和报告。 sed 是一个流编辑器
它们中的每一种都有其自己的功能和特点。

示例
Sed

$ sed -i 's/cat/dog/' file.txt
# this will replace any occurrence of the characters 'cat' by 'dog'

Awk

$ awk '{print $2}' file.txt
# this will print the second column of file.txt

基本 awk 用法:
计算总和/平均值/最大/最小/等。

$ cat file.txt
A 10
B 20
C 60
$ awk 'BEGIN {sum=0; count=0; OFS="\t"} {sum+=$2; count++} END {print "Average:", sum/count}' file.txt
Average:    30

我建议您阅读这本书:Sed & Awk:第 2 版。

它将帮助您在任何类 UNIX 环境中成为熟练的 sed/awk 用户。

Short definition:

grep: search for specific terms in a file

#usage
$ grep This file.txt
Every line containing "This"
Every line containing "This"
Every line containing "This"
Every line containing "This"

$ cat file.txt
Every line containing "This"
Every line containing "This"
Every line containing "That"
Every line containing "This"
Every line containing "This"

Now awk and sed are completly different than grep.
awk and sed 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 editor
Each one of them has its own functionality and specialties.

Example
Sed

$ sed -i 's/cat/dog/' file.txt
# this will replace any occurrence of the characters 'cat' by 'dog'

Awk

$ awk '{print $2}' file.txt
# this will print the second column of file.txt

Basic awk usage:
Compute sum/average/max/min/etc. what ever you may need.

$ cat file.txt
A 10
B 20
C 60
$ awk 'BEGIN {sum=0; count=0; OFS="\t"} {sum+=$2; count++} END {print "Average:", sum/count}' file.txt
Average:    30

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.

凑诗 2024-12-16 18:57:22

如果您想快速搜索文件中匹配的行,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.

紫竹語嫣☆ 2024-12-16 18:57:22

我只想提一件事,有很多工具可以进行文本处理,例如
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文