什么是sed和awk的区别? [英] What is the difference between sed and awk?
问题描述
- 什么是AWK之间的区别
和sed? - 什么样的应用是最好用
对于sed和awk工具的情况下?
推荐答案
SED
是一个流编辑器。它与在每个线的基础上的字符流。它有一个原始的编程语言,包括跳转式循环和简单条件句(除了模式匹配和地址匹配)。基本上有只有两个变量:模式空间和保持空间。脚本的可读性可能是困难的。数学运算是非常尴尬的最好的。
sed
is a stream editor. It works with streams of characters on a per-line basis. It has a primitive programming language that includes goto-style loops and simple conditionals (in addition to pattern matching and address matching). There are essentially only two "variables": pattern space and hold space. Readability of scripts can be difficult. Mathematical operations are extraordinarily awkward at best.
有与命令行选项和语言功能不同程度的支持 SED
的不同版本。
There are various versions of sed
with different levels of support for command line options and language features.
AWK
向在每线的基础上分隔的字段为主。它具有更强大的编程结构,包括如果
/ 其他
,,而
,不要
/ ,而
和
(C-风格和数组迭代)。有变量和单维关联数组加(IMO)kludgey多维数组的完整支持。数学运算类似于那些在C.它的printf
和功能。在K中的AWK代表*的 K 的* ernighan,如Kernighan和Ritchie一书的C程序设计语言的名声(不要忘记*的 A *浩*的是W 的* einberger)。人们可以使用可以想象写学术抄袭的检测 AWK
。
awk
is oriented toward delimited fields on a per-line basis. It has much more robust programming constructs including if
/else
, while
, do
/while
and for
(C-style and array iteration). There is complete support for variables and single-dimension associative arrays plus (IMO) kludgey multi-dimension arrays. Mathematical operations resemble those in C. It has printf
and functions. The "K" in "AWK" stands for "*K*ernighan" as in "Kernighan and Ritchie" of the book "C Programming Language" fame (not to forget *A*ho and *W*einberger). One could conceivably write a detector of academic plagiarism using awk
.
GNU AWK
( GAWK
)拥有众多的扩展,包括在最新的版本真多维数组。有 AWK
的其他变化包括 mawk
和 NAWK
。
GNU awk
(gawk
) has numerous extensions, including true multidimensional arrays in the latest version. There are other variations of awk
including mawk
and nawk
.
这两个程序使用常规的前pressions选择和处理文本。
Both programs use regular expressions for selecting and processing text.
我会倾向于使用 SED
,其中有在文本模式。例如,您可以取代一些文本的形式为减号后面的数字序列(如-231.45)与会计师的括号表单中的所有负数(例如(231.45) )在使用本(其具有改进的余地):
I would tend to use sed
where there are patterns in the text. For example, you could replace all the negative numbers in some text that are in the form "minus-sign followed by a sequence of digits" (e.g. "-231.45") with the "accountant's brackets" form (e.g. "(231.45)") using this (which has room for improvement):
sed 's/-\([0-9.]\+\)/(\1)/g' inputfile
我会使用 AWK
当文本看起来更像行和列,或如 AWK
指他们记录和田。如果我打算做如上类似的操作,但只在一个简单的逗号分隔的文件,我可能会做这样的事情的第三个字段:
I would use awk
when the text looks more like rows and columns or, as awk
refers to them "records" and "fields". If I was going to do a similar operation as above, but only on the third field in a simple comma delimited file I might do something like:
awk -F, 'BEGIN {OFS = ","} {gsub("-([0-9.]+)", "(" substr($3, 2) ")", $3); print}' inputfile
当然,这些只是不说明了全方位的功能,每个所提供的非常简单的例子。
Of course those are just very simple examples that don't illustrate the full range of capabilities that each has to offer.
这篇关于什么是sed和awk的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!