最佳 Awk 命令
我发现 AWK 非常有用。 这是我用来操作数据的单行代码。
ls | awk '{ print "awk " "'"'"'" " {print $1,$2,$3} " "'"'"'" " " $1 ".old_ext > " $1 ".new_ext" }' > file.csh
我使用此 AWK 制作了一个脚本文件,该文件将重命名一些文件并仅打印出选定的列。 有人知道更好的方法吗? 你最擅长的 AWK 的单行或巧妙的操作是什么?
I find AWK really useful. Here is a one liner I put together to manipulate data.
ls | awk '{ print "awk " "'"'"'" " {print $1,$2,$3} " "'"'"'" " " $1 ".old_ext > " $1 ".new_ext" }' > file.csh
I used this AWK to make a script file that would rename some files and only print out selective columns. Anyone know a better way to do this? What are you best AWK one liners or clever manipulations?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
AWK 书充满了很棒的示例。 它们过去是从Kernighan 的网页收集下载的(现在是 404)。
The AWK book is full of great examples. They used to be collected for download from Kernighan's webpage (404s now).
您可以在此处找到一些不错的单行代码。
You can find several nice one liners here.
我使用这个:
汇总系统上跨文件系统使用的所有磁盘空间。
I use this:
To total all disk space used on a system across filesystems.
很多年前,我用 awk 写了一个 tail 脚本:
我知道这很愚蠢,但这就是 awk 对你所做的。 玩起来非常有趣。
Many years ago I wrote a tail script in awk:
It's silly, I know, but that's what awk does to you. It's just very fun playing with it.
Henry Spencer 在 awk 上编写了一个相当不错的 nroff 实现。 他称之为“awf”。 他还声称,如果 Larry Wall 知道 awk 有多么强大,他就不需要发明 perl。
Henry Spencer wrote a fairly good implementation of nroff on awk. He called it "awf". He also claimed that if Larry Wall had known how powerful awk was, he wouldn't have needed to invent perl.
这是我经常使用的几个 awks...请注意,您可以使用 $1、$2 等来获取您想要的列。 因此,为了操作一堆文件,例如,您可以使用一个愚蠢的命令来代替 mv ...
或者如果您正在查看一组进程,也许...
非常微不足道,但您可以看到这将如何帮助您相当多的方法。 =) 我以前做的大部分事情你都可以使用 xargs 来完成,但是嘿,谁需要它们新的命令呢?
Here's a couple of awks that I used to use regularly ... note that you can use $1, $2, etc to get out the column you want. So, for manipulating a bunch of files, for example here's a stupid command you could use instead of mv ...
Or if you're looking at a set of processes maybe ...
Pretty trivial but you can see how that would get you quite a ways. =) Most of the stuff I used to do you can use xargs for, but hey, who needs them new fangled commands?
我经常使用这个脚本来编辑 PATH 和类似路径的环境变量。
用法:
此命令在 PATH 前面添加 /new/bin 和 /other/bin,从 PATH 中删除 /old/bin 和 /other/old/bin(如果存在 - 如果不存在则不会出现错误),并删除重复的目录条目小路。
I use this script a lot for editing PATH and path-like environment variables.
Usage:
This command adds /new/bin and /other/bin in front of PATH, removes both /old/bin and /other/old/bin from PATH (if present - no error if absent), and removes duplicate directory entries on path.
通过替换 httpd来计算 httpd
或任何其他进程使用的内存。 除以 1024 即可得到以 MB 为单位的输出。
Count memory used by httpd
Or any other process by replacing httpd. Dividing by 1024 to get output in MB.
我设法为 UNIX 构建了一个 DOS 树命令模拟器( find + awk ):
I managed to build a DOS tree command emulator for UNIX ( find + awk ):
打印两个模式之间的线条:
详细说明: http://nixtip.wordpress.com/2010/10/12/print-lines- Between-two-patterns-the-awk-way/
Print lines between two patterns:
Detailed explanation: http://nixtip.wordpress.com/2010/10/12/print-lines-between-two-patterns-the-awk-way/
几个最喜欢的,基本上彼此无关。 读作两个不同的、互不相关的建议。
轻松识别列号
:
对于那些经常使用 awk 的人来说,就像我在工作中进行日志分析一样,我经常发现自己需要找出文件的列号。 因此,如果我正在分析 Apache 访问文件(可以在 此处) 我针对该文件运行下面的脚本:
我通常将其称为“cn.awk”,表示“列”和“数字”。 有创意吧? 不管怎样,输出看起来像:
很容易分辨什么是什么。 我通常在我的服务器上使用它的别名并将其放置在各处。
按名称引用字段
现在,假设您的文件有一个标题行,并且您宁愿使用这些名称而不是字段编号。 这允许您这样做:
现在,假设我有这个标题行...
metric,time,val,location,http_status,http_request
...我想对
求和val
列。 我可以通过名称来引用它,而不是引用 $3:主要的好处是使脚本更具可读性。
A couple of favorites, essentially unrelated to each other. Read as 2 different, unconnected suggestions.
Identifying Column Numbers Easily
:
For those that use awk frequently, as I do for log analysis at work, I often find myself needing to find out what the column numbers are for a file. So, if I am analyzing, say, Apache access files (some samples can be found here) I run the script below against the file:
I typically call it "cn.awk", for 'c'olumn 'n'umbers. Creative, eh? Anyway, the output looks like:
Very easy to tell what's what. I usually alias this on my servers and have it everywhere.
Referencing Fields by Name
Now, suppose your file has a header row and you'd rather use those names instead of field numbers. This allows you to do so:
Now, suppose I have this header row...
metric,time,val,location,http_status,http_request
...and I'd like to sum the
val
column. Instead of referring to $3, I can refer to it by name:The main benefit is making the script much more readable.
打印字段是大多数 AWK 教程中首先提到的内容之一。
鲜为人知但同样有用的是排除也可能的字段:
Printing fields is one of the first things mentioned in most AWK tutorials.
Lesser known but equally useful is excluding fields which is also possible: