还有理由学习 AWK 吗?

发布于 2024-07-06 06:55:08 字数 1722 浏览 9 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

椒妓 2024-07-13 06:55:08

如果您快速学习 awk 的基础知识,您确实可以在命令行上做一些令人惊奇的事情。

但学习 awk 的真正原因是有借口阅读 Aho、Kernighan 和 Weinberger 所著的AWK 编程语言精彩一书。

AWK 编程语言,位于 archive.org

从名字上看,你可能会认为它只是教你 awk。 事实上,这只是一个开始。 一旦使用一种简洁的脚本语言,使字符串操作变得容易,就可以解决大量的问题——awk 是第一个——它继续教读者如何实现数据库、解析器和解释器,以及(如果我没记错的话)一个小型项目特定计算机语言的编译器! 如果他们还使用 awk 编写了一个示例操作系统,那么这本书将是对计算机科学的相当完整的概述!

就像原来的 C 语言书籍一样,它以清晰简洁着称,也是友好技术写作的一个很好的例子。 就连索引也是一件艺术品。

哎呀? 如果您了解它,您偶尔会在命令行中使用它,但对于任何更大的东西,您会感到陷入困境,无法访问系统的更广泛功能以及 Python 之类的东西提供的访问权限。 但是书呢? 你会永远很高兴读到它!

If you quickly learn the basics of awk, you can indeed do amazing things on the command line.

But the real reason to learn awk is to have an excuse to read the superb book The AWK Programming Language by Aho, Kernighan, and Weinberger.

The AWK Programming Language at archive.org

You would think, from the name, that it simply teaches you awk. Actually, that is just the beginning. Launching into the vast array of problems that can be tackled once one is using a concise scripting language that makes string manipulation easy — and awk was one of the first — it proceeds to teach the reader how to implement a database, a parser, an interpreter, and (if memory serves me) a compiler for a small project-specific computer language! If only they had also programmed an example operating system using awk, the book would have been a fairly complete survey introduction to computer science!

Famously clear and concise, like the original C Language book, it also is a wonderful example of friendly technical writing done right. Even the index is a piece of craftsmanship.

Awk? If you know it, you'll use it at the command-line occasionally, but for anything larger you'll feel trapped, unable to access the wider features of your system and the Internet that something like Python provides access to. But the book? You'll always be glad you read it!

同展鸳鸯锦 2024-07-13 06:55:08

我认为这取决于您所处的环境。如果您是一个 *nix 人,那么了解 awk 是一件好事。 几乎每个 *nix 上都可以找到的唯一其他脚本环境是 sh。 因此,虽然 grepsed 等肯定可以在现代主流 linux 发行版上取代 awk,但当您转向对于更奇特的系统,了解一点awk将会非常方便。

awk 不仅可以用于文本处理。 例如,我的一位主管用 awk 编写天文学代码 - 这就是他的完全老派很棒。 在他那个时代,这是完成这项工作的最佳工具……现在,即使他的学生像我一样使用 python 之类的东西,他仍然坚持他所知道的并且工作得很好。

最后,世界各地有很多旧代码,知道一点 awk 不会有什么坏处。 它还会让你变得更好*nix人:-)

I think it depends on the environment you find yourself in. If you are a *nix person, then knowing awk is a Good Thing. The only other scripting environment that can be found on virtually every *nix is sh. So while grep, sed, etc can surely replace awk on a modern mainstream linux distro, when you move to more exotic systems, knowing a little awk is going to be Real Handy.

awk can also be used for more than just text processing. For example one of my supervisors writes astronomy code in awk - that is how utterly old school and awesome he is. Back in his days, it was the best tool for the job... and now even though his students like me use python and what not, he sticks to what he knows and works well.

In closing, there is a lot of old code kicking around the world, knowing a little awk isn't going to hurt. It will also make you better *nix person :-)

白龙吟 2024-07-13 06:55:08

我使用 awk 的唯一原因是自动分割:

awk '{print $3}' < file.in

这会打印 file.in 中的第三个空格分隔字段。 这比以下更容易:

tr -s ' ' < file.in | cut -d' ' -f3

The only reason I use awk is the auto-splitting:

awk '{print $3}' < file.in

This prints the third whitespace-delimited field in file.in. It's a bit easier than:

tr -s ' ' < file.in | cut -d' ' -f3
七色彩虹 2024-07-13 06:55:08

我认为如果您的文件包含列/字段,awk 就非常有用。 我在处理/分析多列文件中的特定列时使用它。 或者如果我想添加/删除特定列。

例如,

awk -F \t '{ if ($2 > $3) print; }' <filename>

仅当制表符分隔文件中的第二列值大于第三列值时才会打印。

当然,我可以使用 Perl 或 Python,但 awk 通过简洁的单行命令使其变得更加简单。

而且学习 awk 的成本相当低。 您可以在不到一个小时的时间内学习 awk 基础知识,因此不像学习任何其他编程/脚本语言那么费力。

I think awk is great if your file contains columns/fields. I use it when processing/analyzing a particular column in a multicolumn file. Or if I want to add/delete a particular column(s).

e.g.

awk -F \t '{ if ($2 > $3) print; }' <filename>

will print only if the 2nd column value in a tab seperated file is greater than the 3rd column value.

Of course I could use Perl or Python, but awk makes it so much simpler with a concise single line command.

Also learning awk is pretty low-cost. You can learn awk basics in less than an hour, so it's not as much effort as learning any other programming/scripting language.

ζ澈沫 2024-07-13 06:55:08

我确实经常使用 awk。 它非常适合在管道中间进行非常简单的文本洗牌; 它填补了一个非常狭窄的领域,介于根本不需要它和需要 Perl/Python/任何东西之间。

我不建议您花大量时间在上面,但了解语法的基础知识可能会派上用场——至少足以让您在想要使用它时可以快速查阅手册。

I do use awk every so often. It's good for very simple text shuffling in the middle of a pipeline; it fills a very narrow niche right between not needing it at all and needing to whip out Perl/Python/whatever.

I wouldn't advise you spend a whole lot of time on it, but it might come in handy to know the basics of the syntax -- at least enough that you can consult the manual quickly should you ever want to use it.

白日梦 2024-07-13 06:55:08

我偶尔使用 AWK 来处理 HTML。 例如,此代码将表格转换为 csv 文件:

BEGIN {s=""; FS="n"}
/<td/ { gsub(/<[^>]*>/, ""); s=(s ", " $1);}
/<tr|<TR/ { print s; s="" }

如果您要进行屏幕抓取,这非常有用。 实际上,我喜欢 AWK 可能是因为它让我能够如此快速地构建错误的问题解决方案:) Programming Pearls 中也提到了这一点。

I use AWK occasionally for dealing with HTML. For instance, this code translates tables to csv files:

BEGIN {s=""; FS="n"}
/<td/ { gsub(/<[^>]*>/, ""); s=(s ", " $1);}
/<tr|<TR/ { print s; s="" }

Which is great if you're screen scraping. Actually, it might be the case that I love AWK because it allows me to build the wrong solution to problems so quickly :) more examples. It's also mentioned in Jon Bentley's lovely Programming Pearls.

っ左 2024-07-13 06:55:08

学习 AWK 对我来说非常宝贵,因为我的上一份合同是在嵌入式 Linux 系统上工作,而该系统上既没有安装 Perl,也没有安装大多数其他脚本语言。

Learning AWK was invaluable for me in my last contract working on an embedded Linux system on which neither Perl nor most other scripting languages were installed.

笑咖 2024-07-13 06:55:08

大多数 awk oneliner 都可以使用 Perl oneliner 来实现 - 如果您选择进入 Perl oneliner 思维模式。 或者,只需使用 Perl 三行代码:)

如果您正在维护由喜欢 awk 的人编写的 shell 脚本,那么显然,您将需要学习 awk。

即使没有实际需要,如果您已经了解正则表达式,那么很快就能掌握基础知识,并且了解当时的设计方式很有趣。 是比较优雅的。

Most awk one liners can be achieved with Perl one liners - if you choose to get into a Perl one liner mindset. Or, just use Perl three liners :)

If you're maintaining shell scripts written by someone who liked awk, then clearly, you're going to need to learn awk.

Even if there's no practical need, if you already know regex it won't take long to pick up the basics, and it's fun to see how things were designed back then. It's rather elegant.

失而复得 2024-07-13 06:55:08

在问这个问题 6 年后,我现在可以肯定地回答:不,学习 awk 不值得。

基本任务可以通过基本的 bash 命令甚至 GUI 工具轻松处理。 更复杂的任务可以使用现代动态语言轻松解决,例如 Python(我最喜欢的或我的)或 Ruby。

您绝对应该学习现代脚本动态语言,因为它将帮助您完成许多任务(网络、管理、数据处理、自动化等)。 这样做的话,学习 awk 这样的工具是完全没有用的,它每个月最多只能节省你几秒钟的时间。

6 years after asking this question I can now answer with certainty: no, learning awk is not worth it.

Basic tasks are handled by basic bash commands, or even GUI tools easily. More complex tasks will be easily tackled with modern dynamic languages such as Python (fav or mine) or Ruby.

You should definitely learn a modern scripting dynamic language as it will help you in so many tasks (web, admin, data crunching, automation, etc). And by doing so, learning a tool such as awk is completely useless, it will save you at best a few seconds every month.

大姐,你呐 2024-07-13 06:55:08

如果您已经了解并使用 sed,那么您不妨至少学习一点 awk。 它们可以通过管道连接在一起以实现一些非常强大的技巧。 总是给观众留下深刻的印象。

If you already know and use sed, you might as well pick up at least a bit of awk. They can be piped together for some pretty powerful tricks. Always impresses the audience.

凉风有信 2024-07-13 06:55:08

Computerworld 最近对 Alfred V. Aho(三位创作者之一)进行了采访 AWK)关于AWK。 这是一本非常有趣的读物。 也许您会在其中找到一些提示,了解为什么学习 AWK 是个好主意。

Computerworld recently did an interview with Alfred V. Aho (one of the three creators of AWK) about AWK. It's a quite interesting read. So maybe you'll find some hints in it, why it's a good idea learn AWK.

谜泪 2024-07-13 06:55:08

如果您在编写 shell 脚本时偶尔需要解析日志文件以获取数据或程序输出,那么它非常有用,因为在 awk 中很容易实现,但在 python 中需要多写几行代码。

它的功能当然比这更强大,但这似乎是大多数人使用它的任务。

It's useful mostly if you have to occasionally parse log files for data or output of programs while shell scripting, because it's very easy to achieve in awk that that would take you a little more lines of code in python.

It certainly has more power than that, but this seems to be tasks most people use it for.

混浊又暗下来 2024-07-13 06:55:08

当然:我工作的环境中唯一可用的语言是:(一些生成 COBOL、OMG、OMG 的垃圾语言)、bash(旧版本)、perl(我还没掌握)、sed、awk ,以及其他一些命令行实用程序。 了解 awk 为我节省了几个小时(并且从我的同事那里生成了几个文本处理任务 - 他们每天至少来找我 3 次)。

Of course: I'm working in an environment where the only available languages are: (some shity language which generates COBOL, OMG, OMG), bash (old version), perl (I don't master it yet), sed, awk, and some other command line utilities. Knowing awk saved me several hours (and had generated several text processing tasks from my collegaues - they come to me at least three times a day).

朦胧时间 2024-07-13 06:55:08

awk 具有非常好的实用性/难度比,并且“简单的 awk”适用于每个 Unix/Linux/MacOS(并且它也可以安装在其他系统中)。

它是在黄金时代设计的,当时人们讨厌打字,所以脚本可以非常非常短并且写起来很快。 我会尝试安装mawk,一个快速版本,据称它可以将计算加速大约9倍,awk/gawk相当慢,所以如果你想使用它而不是R等,你可能需要mawk。

awk has a very good ratio utility/difficulty, and "simple awk" works in every Unix/Linux/MacOS (and it can be installed in other systems too).

It was designed in Golden Age when people hated typing, so scripts can be very, very short and fast to write. I will try to instal mawk, a fast version, allegedly it accelerates the computation about 9 times, awk/gawk is rather slow, so if you want to use it instead of R etc. you may want mawk.

白色秋天 2024-07-13 06:55:08

如果您关心速度,但不想处理 C/C++汇编,那么您可以选择 awk,具体来说,<代码>mawk 1.9.9.6。

它还缺少 perl 的丑陋语法、python3 的功能膨胀、javascript 烦人的 UTF16 设置或 C 的内存指针陷阱

大多数情况下,对于相同伪代码的实现,awk 只会输给专门的向量化指令,例如 AVX/SSE

if you care anything about speed, but don't wanna be dealing with C/C++ or assembly, you go for awk, specifically, mawk 1.9.9.6.

It also lacks perl's ugly syntax, python3's feature bloat, javascript's annoying UTF16 setup, or C's memory-pointer pitfall traps

Most of the time, for the implementation of the same pseudo-codes, awk only loses against specialized vectorized instructions, like AVX/SSE

骑趴 2024-07-13 06:55:08

我想说这可能已经不值得了。 我有时会使用它作为比 sed 更通用的流编辑器,并且包含搜索功能,但是如果您精通 python,我不知道您能够更快地完成哪个任务以补偿所需的时间学习awk。

以下命令可能是我在过去两年中使用 awk 的唯一命令(它从我的 Debian/Ubuntu 系统中清除了一半删除的软件包):

$ dpkg -l|awk '/^rc/ {print $2}'|xargs sudo dpkg -P

I'd say it's probably not worth it anymore. I use it from time to time as a much more versatile stream editor than sed with searching abilities included, but if you are proficient with python I do not know a task which you would be able to finish that much faster to compensate for the time needed to learn awk.

The following command is probably the only one for which I've used awk in the last two years (it purges half-removed packages from my Debian/Ubuntu systems):

$ dpkg -l|awk '/^rc/ {print $2}'|xargs sudo dpkg -P
千鲤 2024-07-13 06:55:08

没有。

尽管它可能很有趣,但您可以使用其他更强大的工具(例如 Perl)来完成 awk 可以做的所有事情。

花时间学习那些更强大的工具 - 并且只是顺便学习一些 awk。

Nope.

Even though it might be interesting, you can do everything that awk can do using other, more powerful tools such as Perl.

Spend your time learning those more powerful tools - and only incidentally pick up some awk along the way.

笛声青案梦长安 2024-07-13 06:55:08

现在 PERL 已被移植到几乎所有重要的平台,我认为这是不值得的。 它比 sed 和 awk 一起使用更通用。 至于自动分割,您可以像这样在 perl 中进行:

perl -F':' -ane 'print $F[3],"\n";' /etc/passwd

编辑:您可能仍然想稍微熟悉一下 awk,因为其他一些工具是基于其基于模式的操作的哲学(例如 Solaris 上的 DTrace)。

Now that PERL is ported to pretty much every significant platform, I'd say it's not worth it. It's more versatile than sed and awk together. As for auto-splitting, you can do it in perl like this:

perl -F':' -ane 'print $F[3],"\n";' /etc/passwd

EDIT: you might still want to get somewhat acquainted with awk, because some other tools are based on its philosophy of pattern-based actions (e.g. DTrace on Solaris).

酒绊 2024-07-13 06:55:08

我想说有。 对于简单的事情,对于缺乏经验的系统管理员/开发人员来说,AWK 比 Python 容易得多。 你可以学一点 AWK 并做很多事情,学习 Python 意味着学习一门全新的语言(是的,我知道 AWK 是一种语言也是一种感觉)。

Perl 或许可以做很多 AWK 可以做的事情,但在当今时代我会选择 Python。 所以是的,你应该学习 AWK。 但也要学习Python:-)

I'd say there is. For simple stuff, AWK is a lot easier on the inexperienced sysadmin / developer than Python. You can learn a little AWK and do a lot of things, learning Python means learning a whole new language (yes, I know AWK is a language is a sense too).

Perl might be able to do a lot of things AWK can do, but offered the choice in this day and age I would choose Python here. So yes, you should learn AWK. but learn Python too :-)

向日葵 2024-07-13 06:55:08

我最近试图可视化记录 DOS 攻击的网络 pcap 文件,其大小超过 20Gbs。 我需要时间戳和 IP 地址。 在我的场景中,AWK 一行代码运行得非常好,而且速度也相当快。 我专门使用 AWK 来清理提取的文件,获取 IP 地址以及分组时间范围内这些 IP 地址的数据包总数。 我完全同意上面其他人写的内容。 这取决于您的需求。

I was recently trying to visualize network pcap files logging a DOS attack which amounted to over 20Gbs. I needed the timestamp and the Ip addresses. In my scenario, AWK one-liner worked fabulously and pretty fast as well. I specifically used AWK to clean the extracted files, get the ip addresses and the total packet count from those IP addresses within grouped span of time. I totally agree with what other people have written above. It depends on your needs.

一片旧的回忆 2024-07-13 06:55:08

awk 是一种强大的工具语言,因此,如果您是任何类型的 IT 专业人士,您可能会发现在某些地方使用 awk。 如果您可以处理 grepsed 的语法和正则表达式,那么您学习 awk 应该没有问题,而且可能值得这样做。

我发现 awk 真正出色的地方在于简化了诸如处理多行记录和同时修改/插入多个文件之类的事情。

awk is a powertool language, so you are likely going to find awk being used somewhere if you are an IT professional of any sort. If you can handle the syntax and regular expressions of grep and sed then you should have no problem picking up awk and it is probably worthwhile to.

Where I've found awk really shine is in simplifying things like processing multi-line records and mangling/interpolating multiple files simultaneously.

梦里°也失望 2024-07-13 06:55:08

不学习 awk 的原因之一是它在正则表达式中没有非贪婪匹配。

我有一个 awk 代码,现在我必须重写它,只是因为我突然调试到 awk/gawk 中不存在非贪婪匹配之类的东西,因此它无法正确执行某些正则表达式。

One reason NOT to learn awk is that it doesn't have non-greedy matches in regular expressions.

I have an awk code that now I must rewrite only because I suddenly debugged that there is no such thing as non-greedy matches in awk/gawk thus it can't properly execute some regexes.

还不是爱你 2024-07-13 06:55:08

这取决于您的队友和领导者以及您正在执行的任务。

if( team mates and leader ask to write awk ){
  if( you can reject that){
    if( awk code is very small){
      learn little just like learn Regex
    }else{
      use python or even java
    }
  }else{
    do as they ask
  }
}

It depends on your team mates and you leader and the task you are working on.

if( team mates and leader ask to write awk ){
  if( you can reject that){
    if( awk code is very small){
      learn little just like learn Regex
    }else{
      use python or even java
    }
  }else{
    do as they ask
  }
}
百变从容 2024-07-13 06:55:08

我在文件格式为列的区域工作。 因此 awk 对我来说非常有用,可以重新格式化文件,以便不同的软件可以一起工作。 对于非IT职业来说,使用awk就足够而且完美了。 现在,计算机速度不再是问题,所以我可以将 awk 和 awk 结合起来。 unix 将许多 1liners 命令通过管道传输到“脚本”中。 通过 Awk 按字段和记录搜索,我用它来非常快速地检查文件数据,而不是使用“vi”打开文件。 我不得不说 awk 的能力给我的工作带来了特别的乐趣,我能够使用 awk 帮助同事快速解决问题。 对我来说很棒的代码。

I work in area the files are in column format. So awk is invaluable to me to REFORMAT the file so different software can work together. For non IT profession, using awk is enough and perfect. Now a day, computer speed is not an issue, so I can combine awk & unix to pipe many 1 liners command into a "script". With Awk search by field and record, I use it to check the file data very fast, instead of "vi" to open a file. I have to say awk capability brought joy to my job specially, I am able to assist co-worker to sort things out quickly using awk. Amazing code to me.

日裸衫吸 2024-07-13 06:55:08

我目前一直在用 python 进行一些编码。
但我仍然不太了解它,无法轻松地使用它进行简单的一次性文件转换。

使用 awk,我可以在 unix 命令行上快速开发一行代码,执行一些非常漂亮的转换。 每次我使用 awk 时,我编写的代码都是一次性的,长度不会超过几行。 也许在一行上有一个“if”语句和“printf”语句。

我从来没有用awk写过超过10行的代码。
我几年前就看过一些这样的剧本。

但任何需要多行代码的事情,我都会求助于 python。

我喜欢 awk。 它与 sed 结合是一个非常强大的工具。

I have been doing some coding in python at present.
But I still do not know it well enough to use easily for simple one off file transformations.

With awk I can quickly develop a one line piece of code on the unix command line that does some pretty swish transformations. Every time I use awk, the piece of code I write will be disposable and no more than a few lines long. Maybe an "if" statment and "printf" statement here or there on the one line.

I have never written a piece of code that is more than 10 lines long with awk.
I saw some such scripts years ago.

But anything that required many lines of code, I would resort to python.

I love awk. It is a very powerful tool in combination with sed.

清醇 2024-07-13 06:55:08

在我看来,它是一个具有足够功能来完成工作的工具。 在 IT 领域的大多数情况下,您确实需要更多。

我从其他人那里学到的简单规则

如果你可以用脚本做到这一点,你就不应该使用 C
如果可以使用 awk,则永远不要使用脚本或脚本语言;
如果可以用 sed 实现,就不要使用 awk;
如果可以用 grep 来完成,就不要使用 sed。

IMO it’s a tool that has enough features to get things done. In most cases in IT do you really need more.

Simple rule I learned from others

You should never use C if you can do it with a script
You should never use a script or scripting language if you can do it with awk;
Never use awk if you can do it with sed;
Never use sed if you can do it with grep.

青衫负雪 2024-07-13 06:55:08

awk 可以轻松地收集和聚合数据,而 sed 和 grep 则无法做到这一点。 我经常必须查看从(许多)远程设备收集的日志,并且我想要诸如“哪些服务器使用参数 x > 记录了以下消息”之类的答案。 过去 24 小时内有 1000 个。 一旦了解了基础知识,就可以很容易地在命令行上编写这样的内容。 或者,如果您需要重用它,请创建一个 awk 脚本。

What awk can do easily that sed and grep cannot is collect and aggregate data. I often have to look at logs gathered from (many) remote devices and I want answers like 'which servers have logged the following message with parameter x > 1000 in the last 24 hours'. Once you know the basics, it's pretty easy to write stuff like this on the command line. Or create an awk script if you'll need to reuse it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文