sed 和 awk 有什么区别? [英] What is the difference between sed and awk?

查看:17
本文介绍了sed 和 awk 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • awk 和 awk 的区别是什么和 sed ?
  • 最好使用什么样的应用程序sed 和 awk 工具的案例?

推荐答案

sed 是一个流编辑器.它以每行为基础处理字符流.它有一个原始的编程语言,包括 goto 风格的循环和简单的条件(除了模式匹配和地址匹配).本质上只有两个变量":模式空间和保持空间.脚本的可读性可能很困难.数学运算充其量是非常笨拙的.

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 面向基于每行的分隔字段.它有更健壮的编程结构,包括if/elsewhiledo/whilecode> 和 for(C 风格和数组迭代).完全支持变量和单维关联数组加上 (IMO) kludgey 多维数组.数学运算类似于 C 中的运算.它有 printf 和函数.AWK"中的K"代表Kernighan",就像C Programming Language"一书的Kernighan and Ritchie"一样(不要忘记Aho和 Weinberger).可以想象,人们可以使用 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 "Kernighan" as in "Kernighan and Ritchie" of the book "C Programming Language" fame (not to forget Aho and Weinberger). One could conceivably write a detector of academic plagiarism using awk.

GNU awk (gawk) 有许多扩展,包括最新版本中的真正多维数组.awk 还有其他变体,包括 mawknawk.

GNU awk (gawk) has numerous extensions, including true multidimensional arrays in the latest version. There are other variations of awk including mawk and nawk.

两个程序都使用正则表达式来选择和处理文本.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆