Perl、Python、AWK 和 sed 之间有什么区别? [英] What are the differences between Perl, Python, AWK and sed?

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

问题描述

它们之间的主要区别是什么?以及在哪些典型场景下使用每种语言更好?

What are the main differences among them? And in which typical scenarios is it better to use each language?

推荐答案

按出现顺序,语言为sedawkperl, python.

In order of appearance, the languages are sed, awk, perl, python.

sed 程序是一个流编辑器,旨在将脚本中的操作应用到输入文件的每一行(或更一般地,应用到指定范围的行).它的语言基于ed,Unix 编辑器,虽然它有条件等,但很难处理复杂的任务.你可以用它创造小奇迹——但代价是你头上的头发.但是,在尝试其职权范围内的任务时,它可能是最快的程序.(它具有所讨论程序中最不强大的正则表达式 - 足以满足多种用途,但肯定不是 PCRE - Perl 兼容的正则表达式)

The sed program is a stream editor and is designed to apply the actions from a script to each line (or, more generally, to specified ranges of lines) of the input file or files. Its language is based on ed, the Unix editor, and although it has conditionals and so on, it is hard to work with for complex tasks. You can work minor miracles with it - but at a cost to the hair on your head. However, it is probably the fastest of the programs when attempting tasks within its remit. (It has the least powerful regular expressions of the programs discussed - adequate for many purposes, but certainly not PCRE - Perl-Compatible Regular Expressions)

awk 程序(名称来自其作者的首字母 - Aho、Weinberger 和 Kernighan)最初是用于格式化报告的工具.它可以用作增强的 sed;在其较新的版本中,它在计算上是完整的.它使用了一个有趣的想法——程序基于模式匹配"和模式匹配时采取的行动".这些模式相当强大(扩展正则表达式).操作的语言类似于 C.awk 的主要特性之一是它将输入自动拆分为记录,并将每个记录拆分为字段.

The awk program (name from the initials of its authors - Aho, Weinberger, and Kernighan) is a tool initially for formatting reports. It can be used as a souped-up sed; in its more recent versions, it is computationally complete. It uses an interesting idea - the program is based on 'patterns matched' and 'actions taken when the pattern matches'. The patterns are fairly powerful (Extended Regular Expressions). The language for the actions is similar to C. One of the key features of awk is that it splits the input automatically into records and each record into fields.

Perl 部分是作为 awk-killer 和 sed-killer 编写的.它提供的两个程序是a2ps2p,用于将awk 脚本和sed 脚本转换为Perl.Perl 是最早的下一代脚本语言之一(Tcl/Tk 可能会称霸).它具有强大的集成正则表达式处理和更强大的语言.它提供对几乎所有系统调用的访问,并具有 CPAN 模块的可扩展性.(awksed 都不可扩展.)Perl 的座右铭之一是TMTOWTDI - 有不止一种方法可以做到"(发音为tim-toady").Perl 有对象",但它更像是一个附加组件,而不是语言的基本部分.

Perl was written in part as an awk-killer and sed-killer. Two of the programs provided with it are a2p and s2p for converting awk scripts and sed scripts into Perl. Perl is one of the earliest of the next generation of scripting languages (Tcl/Tk can probably claim primacy). It has powerful integrated regular expression handling with a vastly more powerful language. It provides access to almost all system calls and has the extensibility of the CPAN modules. (Neither awk nor sed is extensible.) One of Perl's mottos is "TMTOWTDI - There's more than one way to do it" (pronounced "tim-toady"). Perl has 'objects', but it is more of an add-on than a fundamental part of the language.

Python 是最后编写的,部分原因可能是对 Perl 的反应.它有一些有趣的句法思想(缩进表示级别 - 没有大括号或等价物).它比 Perl 更本质上是面向对象的;它与 Perl 一样具有可扩展性.

Python was written last, and probably in part as a reaction to Perl. It has some interesting syntactic ideas (indenting to indicate levels - no braces or equivalents). It is more fundamentally object-oriented than Perl; it is just as extensible as Perl.

好的 - 什么时候使用?

OK - when to use each?

  • Sed - 当您需要对文件进行简单的文本转换时.
  • Awk - 当您只需要简单的格式化和汇总或数据转换时.
  • Perl - 几乎适用于任何任务,尤其是当任务需要复杂的正则表达式时.
  • Python - 用于与 Perl 相同的任务.

我不知道 Perl 能做什么而 Python 不能,反之亦然.两者之间的选择将取决于其他因素.我在有 Python 之前就学习了 Perl,所以我倾向于使用它.Python 的语法较少,而且通常更容易学习.Perl 6,当它可用时,将是一个引人入胜的发展.

I'm not aware of anything that Perl can do that Python can't, nor vice versa. The choice between the two would depend on other factors. I learned Perl before there was a Python, so I tend to use it. Python has less accreted syntax and is generally somewhat simpler to learn. Perl 6, when it becomes available, will be a fascinating development.

(请注意,特别是 Perl 和 Python 的概述"非常不完整;可以写整本书来讨论这个主题.)

(Note that the 'overviews' of Perl and Python, in particular, are woefully incomplete; whole books could be written on the topic.)

这篇关于Perl、Python、AWK 和 sed 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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