Perl,Python,AWK和sed有什么区别?[关闭]

问题:Perl,Python,AWK和sed有什么区别?[关闭]

只想知道它们之间的主要区别是什么?以及每种语言的功能(最好使用它)。

编辑:不是“ vs”。就像话题,只是信息。

just want to know what are the main differences among them? and the power of each language (where it’s better to use it).

Edit: it’s not “vs.” like topic, just information.


回答 0

在出现的顺序,语言是sedawkperlpython

sed程序是一个流编辑器,旨在将脚本中的操作应用于输入文件的每一行(或更一般而言,应用于指定的行范围)。它的语言基于edUnix编辑器,尽管它具有条件等,但是很难处理复杂的任务。您可以用它创造一些小奇迹-但要花很多钱。但是,当尝试在其权限范围内执行任务时,它可能是最快的程序。(它具有所讨论程序的功能最弱的正则表达式-足以满足许多目的,但肯定不是PCRE-与Perl兼容的正则表达式)

awk程序(以其作者的名字缩写命名-Aho,Weinberger和Kernighan)最初是用于格式化报告的工具。它可以用作汤sed。在最新版本中,它在计算上是完整的。它使用了一个有趣的想法-该程序基于“模式匹配”和“模式匹配时采取的动作”。这些模式非常强大(扩展正则表达式)。动作的语言类似于C。的主要功能之一awk是它将输入自动分为记录,每个记录又分为字段。

Perl的部分编写是awk杀手和sed杀手。附带的两个程序是a2ps2p用于将awk脚本和sed脚本转换为Perl。Perl是下一代脚本语言中最早的一种(Tcl / Tk可能声称是首要的)。它具有功能强大的集成正则表达式处理功能,并且语言功能强大得多。它提供对几乎所有系统调用的访问,并具有CPAN模块的可扩展性。(既不是可扩展的,awk也不sed是可扩展的。)Perl的座右铭之一是“ TMTOWTDI-做到这一点的方法不止一种”(发音为“ tim-toady”)。Perl有“对象”,但它不仅仅是语言的基本组成部分,而是附加组件。

Python是最后编写的,可能部分是对Perl的反应。它具有一些有趣的句法概念(缩进以指示级别-没有大括号或等效项)。从根本上说,它比Perl更面向对象。它与Perl一样可扩展。

OK-什么时候使用每个?

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

我不知道Perl可以做Python无法做到的任何事情,反之亦然。两者之间的选择将取决于其他因素。我在没有Python之前就学习了Perl,所以我倾向于使用它。Python的语法较少,而且通常更易于学习。Perl 6上市后,将是一个引人入胜的发展。

(请注意,尤其是Perl和Python的“概述”还很不完整;整本书都可以写成该主题。)

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

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)

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 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 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 – when you need to do simple text transforms on files.
  • Awk – when you only need simple formatting and summarisation or transformation of data.
  • Perl – for almost any task, but especially when the task needs complex regular expressions.
  • Python – for the same tasks that you could use Perl for.

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.

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


回答 1

掌握了数十种语言后,您会厌倦像S. Lott这样的人(请参阅他对此问题的有争议的答案,在回答六年后,向下投票的比例几乎是向上投票的一半(+ 45 / -22))。

Sed是用于极其简单的命令行管道的最佳工具。在sed管理员的手中,它适合一次性复杂的应用程序,但除非常简单的替换管道外,不应在生产代码中使用。诸如“ s / this / that /”之类的东西。

当只有一个输入源和一个输出(或依次写入的多个输出)时,Gawk(GNU awk)是进行复杂数据重新格式化的最佳选择。由于大量实际工作都符合此描述,并且优秀的程序员可以在两个小时内学习gawk,因此这是最佳选择。在这个星球上,更简单,更快更好!

当您有非常复杂的输入/输出方案时,Perl或Python远远优于任何版本的awk或sed。从维护和可读性的角度来看,问题越复杂,使用python的效果就越好。但是请注意,优秀的程序员可以用任何语言编写可读的代码,而糟糕的程序员可以用任何有用的语言编写无法维护的废话,因此,如果说Perl或python是程序员,则可以放心地选择perl或python熟练而聪明。

After mastering a few dozen languages, you get tired of people like S. Lott (see his controversial answer to this question, nearly half as many down-votes as up (+45/-22) six years after answering).

Sed is the best tool for extremely simple command-line pipelines. In the hands of a sed master, it’s suitable for one-offs of arbitrary complexity, but it should not be used in production code except in very simple substitution pipelines. Stuff like ‘s/this/that/.’

Gawk (the GNU awk) is by far the best choice for complex data reformatting when there is only a single input source and a single output (or, multiple outputs sequentially written). Since a great deal of real-world work conforms to this description, and a good programmer can learn gawk in two hours, it is the best choice. On this planet, simpler and faster is better!

Perl or Python are far better than any version of awk or sed when you have very complex input/output scenarios. The more complex the problem is, the better off you are using python, from a maintenance and readability standpoint. Note, however, that a good programmer can write readable code in any language, and a bad programmer can write unmaintainable crap in any useful language, so the choice of perl or python can safely be left to the preferences of the programmer if said programmer is skilled and clever.


回答 2

我不会将sed称为成熟的编程语言,它是一种流编辑器,具有旨在以编程方式编辑文本文件的语言构造。

Awk只是一种通用语言,但它仍然最适合于文本处理。

Perl和Python是成熟的通用编程语言。Perl扎根于文本处理,并具有许多类似awk的构造(甚至网上都有awk-to-perl脚本浮动)。Perl和Python之间有很多区别,您最好的选择可能是在Wikipedia等上阅读两种语言的摘要,以更好地了解它们的含义。

I wouldn’t call sed a fully-fledged programming language, it is a stream editor with language constructs aimed at editing text files programmatically.

Awk is a little more of a general purpose language but it is still best suited for text processing.

Perl and Python are fully fledged, general purpose programming languages. Perl has its roots in text processing and has a number of awk-like constructs (there is even an awk-to-perl script floating around on the net). There are many differences between Perl and Python, your best bet is probably to read the summaries of both languages on something like Wikipedia to get a good grasp on what they are.


回答 3

首先,列表“ Perl,Python awk和sed”中有两个不相关的内容。

事情1-简化的文本操作工具。

  • sed。它具有固定,相对简单的工作范围,该范围由读取和检查文件的每一行的想法定义。sed并非旨在使其特别可读。它被设计为在非常小的Unix服务器上非常小且非常高效。

  • w 它的固定性稍差,工作范围也较简单。但是,awk程序的主循环是通过隐式读取源文件的行来定义的。

这些不是“完整的”编程语言。尽管您可以通过一些工作用awk编写相当复杂的程序,但它很快变得复杂且难以阅读。

第2件事-通用编程语言。它们具有丰富的语句类型,大量的内置数据结构,并且没有任何固定的假设或捷径可言。

  • Perl。

  • Python。

什么时候使用它们。

  • sed。决不。在内存超过32K的现代计算机中,它确实没有任何价值。Perl或Python更清楚地执行了相同的操作。

  • w 决不。像sed一样,它反映了更早的计算时代。与其维护这种语言(除了一个成功的系统所需的所有其他语言),不如简单地用一种令人愉悦的语言来做所有事情。

  • Perl。任何形式的编程问题。如果您喜欢自由思考的语法,并且有很多很多方法可以执行相同的操作,那么perl很有趣。

  • Python。任何形式的编程问题。如果您喜欢语法相当有限的语法,那么其中的选择更少,细节更少,并且(也许)更加清晰。Python的面向对象特性使其更适合于大型复杂问题。

背景-我并不是因为无知而抨击sed和awk。我20多年前学过awk。它做了很多事情;曾经将其作为Unix的核心技能来教授。我大约15年前学习了Perl。做了很多复杂的事情。我把两者都抛在后面是因为我可以在Python中做同样的事情-而且更简单,更清晰。

sed和awk有两个严重的问题,都不是年龄。

  1. 其实施不完整。sed和awk所做的一切都可以在Python或Perl中完成,通常更简单,有时也更快。Shell管道由于具有多重处理而具有一些性能优势。Python提供了一个subprocess模块,使我可以恢复这些优势。

  2. 需要学习另一种语言。通过使用Python(或Perl)执行操作,您的实现依赖于更少的语言,从而提高了清晰度。

First, there are two unrelated things in the list “Perl, Python awk and sed”.

Thing 1 – simplistic text manipulation tools.

  • sed. It has a fixed, relatively simple scope of work defined by the idea of reading and examining each line of a file. sed is not designed to be particularly readable. It is designed to be very small and very efficient on very tiny unix servers.

  • awk. It has a slightly less fixed, less simple scope of work. However, the main loop of an awk program is defined by the implicit reading of lines of a source file.

These are not “complete” programming languages. While you can — with some work — write fairly sophisticated programs in awk, it rapidly gets complicated and difficult to read.

Thing 2 – general-purposes programming languages. These have a rich variety of statement types, numerous built-in data structures, and no wired-in assumptions or shortcuts to speak of.

  • Perl.

  • Python.

When to use them.

  • sed. Never. It really doesn’t have any value in the modern era of computers with more than 32K of memory. Perl or Python do the same things more clearly.

  • awk. Never. Like sed, it reflects an earlier era of computing. Rather than maintain this language (in addition to all the other required for a successful system), it’s more pleasant to simply do everything in one pleasant language.

  • Perl. Any programming problem of any kind. If you like free-thinking syntax, where there are many, many ways to do the same thing, perl is fun.

  • Python. Any programming problem of any kind. If you like fairly limited syntax, where there are fewer choices, less subtlety, and (perhaps) more clarity. Python’s object-oriented nature makes it more suitable for large, complex problems.

Background — I’m not bashing sed and awk out of ignorance. I learned awk over 20 years ago. Did many things with it; used to teach it as a core unix skill. I learned Perl about 15 years ago. Did many sophisticated things with it. I’ve left both behind because I can do the same things in Python — and it is simpler and more clear.

There are two serious problems with sed and awk, neither of which are their age.

  1. The incompleteness of their implementation. Everything sed and awk do can be done in Python or Perl, often more simply and sometimes faster, too. A shell pipeline has some performance advantages because of its multi-processing. Python offers a subprocess module to allow me to recover those advantages.

  2. The need to learn yet another language. By doing things in Python (or Perl) your implementation depends on fewer languages, with a resulting increase in clarity.


回答 4

何时使用它们:awk-永远不会-S. Lott。

我认为洛特(S. Lott)对此建议略有遗漏。事实是,在Linux和其他UNIX环境上,awk是与bash,sh和ksh一起用于快速文本处理的有用工具。脚本本身的想法是,您可以通过将该工具(即该工具)粘合在一起来解决问题。因此,在管理脚本中,通常具有ls,grep,|,awk,time,ps等。每个脚本编写器都将其组合为一个工具,就像构建器一砖一瓦地完成建筑(解决当前的问题) 。

例如,我是团队管理的团队成员 彩弹装备互联网。该电子商务网站基于LAMP堆栈。为了自动处理和标准化来自各个供应商的数据馈入后端数据库,我们采用并维护了多种脚本组合,包括bash,perl,php甚至期望。每个模块都有其优势,这取决于可用的模块和API。在bash脚本中,我们使用awk进行模式的快速模式匹配和适当的操作,而无需切换到PERL。我还想指出的一件事是,这些脚本中有相当一部分是从开放源代码中购买或获得的,而在线程中并未强调。如果脚本以Perl的形式出现,则我们将其保留为Perl;如果脚本以Php格式出现,我们将其保持为Php;如果它是bash,我们将其保持为bash;

When to use them: awk – never – S. Lott.

I think S. Lott slightly missed the mark with this recommendation. The fact is, on Linux and the other UNIX environments, awk is a useful tool to be used with bash, sh, and ksh for quick text processings. The idea of scripting itself is you solve your problem by gluing together this tool, that tool. Hence in admin scripts, it is common to has ls, grep, |, awk, time, ps, etc. Each is a tool that the scripter combines like a builder brick by brick to finish the building (to solve the problem at hand).

For instance I am a team member of the team managing paintball gear supplies dotcom. This e-commerce site is based on the LAMP stack. For automated processing and normalizing data feeds from various suppliers into the back end database, we employ and maintain a diversified mix of scripts, including bash, perl, php, and even expect. Each has its strengths based on the available modules and API. In the bash scripts we do quick patterns match and appropriate actions on the patterns as needed using awk without the need to switch to PERL. One thing I would also like to point out, which has not been emphasized in the thread, is that a fair number of these scripts were purchased, or gotten from the open source. If the script came as Perl, we maintain it as Perl; if the script came as Php, we maintain it as Php; if it came as bash, we maintain it as bash; we do not re-write it in another language just because we think it is less efficient in the original language.