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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7727640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:36:59  来源:igfitidea点击:

What are the differences among grep, awk & sed?

linuxbashsedawkgrep

提问by user288609

Possible Duplicate:
What are the differences between Perl, Python, AWK and sed?
What is the difference between sed and awk?

可能的重复:
Perl、Python、AWK 和 sed 之间有什么区别?
sed 和 awk 有什么区别?

Maybe not a very specific question, but I am confused about the differences between grep, awkand sedin terms of their role in Unix/Linux system administration and text processing.

也许不是一个很具体的问题,但我感到困惑之间的差异grepawksed在他们的Unix / Linux系统管理和文本处理的作用方面。

采纳答案by aayoubi

Short definition:

简短定义:

grep: search for specific terms in a file

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"

Now awkand sedare completly different than grep. awkand sedare 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).

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

awkis mostly used for data extraction and reporting. sedis a stream editor
Each one of them has its own functionality and specialties.

awk主要用于数据提取和报告。sed是一个流编辑器,
每一个都有自己的功能和专长。

Example
Sed

示例
Sed

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

Awk

awk

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

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

基本awk用法:
计算 sum/average/max/min/etc。你可能需要什么。

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

I recommend that you read this book: Sed & Awk: 2nd Ed.

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

It will help you become a proficient sed/awk user on any unix-like environment.

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

回答by Kurt Stutsman

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.

如果您想快速搜索文件中匹配的行,则 Grep 很有用。它还可以返回一些其他简单的信息,如匹配行号、匹配计数和文件名列表。

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.

Awk 是一种完整的编程语言,围绕读取 CSV 样式的文件、处理记录以及可选地打印结果数据集而构建。它可以做很多事情,但它不是用于简单任务的最简单的工具。

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.

当您想根据正则表达式更改文件时,sed 很有用。它使您可以轻松匹配部分行、进行修改并打印结果。它的表现力不如 awk,但这使它更容易用于简单的任务。它有许多您可以使用的更复杂的运算符(我认为它甚至是图灵完备的),但通常您不会使用这些功能。

回答by Kent

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.....

我只想提一件事,有很多工具可以做文本处理,例如排序、剪切、拆分、连接、粘贴、通信、uniq、列、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. :)

一种学习文本处理的懒惰方式(不是最好的方式)可能是:只学习 grep 、 sed 和 awk 。使用这三个工具,您可以解决几乎 99% 的文本处理问题,而无需记住以上不同的 cmds 和选项。:)

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.

一种更懒惰的方法可能是学习一种脚本语言(python、perl 或 ruby​​)并使用它进行所有文本处理。