From d506be31ef648dfa5ca7256c7e5cf090df57ccce Mon Sep 17 00:00:00 2001 From: LuMing <784315443@qq.com> Date: Sun, 8 Dec 2019 10:00:56 +0800 Subject: [PATCH] translated --- .../tech/20191125 The many faces of awk.md | 234 ------------------ .../tech/20191125 The many faces of awk.md | 219 ++++++++++++++++ 2 files changed, 219 insertions(+), 234 deletions(-) delete mode 100644 sources/tech/20191125 The many faces of awk.md create mode 100644 translated/tech/20191125 The many faces of awk.md diff --git a/sources/tech/20191125 The many faces of awk.md b/sources/tech/20191125 The many faces of awk.md deleted file mode 100644 index ec0c5b1b09..0000000000 --- a/sources/tech/20191125 The many faces of awk.md +++ /dev/null @@ -1,234 +0,0 @@ -[#]: collector: (lujun9972) -[#]: translator: (luuming) -[#]: reviewer: ( ) -[#]: publisher: ( ) -[#]: url: ( ) -[#]: subject: (The many faces of awk) -[#]: via: (https://www.networkworld.com/article/3454979/the-many-faces-of-awk.html) -[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/) - -The many faces of awk -====== -The awk command provides a lot more than simply selecting fields from input strings, including pulling out columns of data, printing simple text, evaluating content – even doing math. -Thinkstock - -If you only use **awk** when you need to select a specific field from lines of text, you might be missing out on a lot of other services that the command can provide. In this post, we'll look at this simple use along with some of the other things that **awk** can do for you and provide some examples. - -### Plucking out columns of data - -The easiest and most commonly used service that **awk** provides is selecting specific fields from files or from data that is piped to it. With the default of using white space as a field separator, this is very simple. - -[[Get regularly scheduled insights by signing up for Network World newsletters.]][1] - -``` -$ echo one two three four five | awk ‘{print $4}’ -four -$ who | awk ‘{print $1}’ -jdoe -fhenry -``` - -White space is any sequence of blanks and tabs. In the commands shown above, **awk** is extracting just the fourth and first fields from the data provided. - -[][2] - -BrandPost Sponsored by HPE - -[Take the Intelligent Route with Consumption-Based Storage][2] - -Combine the agility and economics of HPE storage with HPE GreenLake and run your IT department with efficiency. - -Awk can also pull text from files by just adding the name of the file after the **awk** command. - -``` -$ awk '{print $1,$5,$NF}' HelenKellerQuote -The beautiful heart. -``` - -In this case, **awk** has picked out the first, fifth and last words in the single line of test. - -The **$NF** specification in the command picks the last piece of text on each line. That is because **NF** represents the number of fields in a line (23) while **$NF** then represents the _value_ of that field ("heart."). The period is included because it's part of the final text string. - -Fields can be printed in any order that you might find useful. In this example, we are rearranging the fields in **date** command output. - -``` -$ date | awk '{print $4,$3,$2}' -2019 Nov 22 -``` - -If you omit the commas between the field designators in an **awk** command, the output will be pushed into a single string. - -``` -$ date | awk '{print $4 $3 $2}' -2019Nov21 -``` - -If you replace the usual commas with hyphens, **awk** will attempt to subtract one field from another – probably not what you intended. It doesn't take the hyphens as characters to be inserted into the print output. Instead, it puts some of its mathematical prowess into play. - -``` -$ date | awk '{print $4-$3-$2}' -1997 -``` - -In this case, it's subtracting 22 (the day of the month) from the year (2019) and simply ignoring "Nov". - -If you want your output to be separated by something other than white space, you can specify your output separator with **OFS** (output field separator) like this: - -``` -$ date | awk '{OFS="-"; print $4,$3,$2}' -2019-Nov-22 -``` - -### Printing simple text - -You can also use **awk** to simply display some text. Of course, if all you want to do is print a line of text, you'd be better off using an **echo** command. On the other hand, as part of an **awk** script, printing some relevant text can be very useful. Here's a practically useless example: - -``` -$ awk 'BEGIN {print "Hello, World" }' -Hello, World -``` - -Here's a more sensible example in which adding a line of text to label your data can help identify what you're looking at: - -``` -$ who | awk 'BEGIN {print "Current logins:"} {print $1}' -Current logins: -shs -nemo -``` - -### Specifying a field separator - -Not all input is going to be separated by white space. If your text is separated by some other character (e.g., commas, colons or semicolons), you can inform **awk** by using the **-F** (input separator) option as shown here: - -``` -$ cat testfile -a:b:c,d:e -$ awk -F : '{print $2,$3}' testfile -b c,d -``` - -Here's a more useful example – pulling a field from the colon-separated **/etc/passwd** file: - -``` -$ awk -F: '{print $1}' /etc/passwd | head -11 -root -daemon -bin -sys -sync -games -man -lp -mail -news -uucp -``` - -### Evaluating content - -You can also evaluate fields using **awk**. If you, for example, want to list only _user accounts_ in **/etc/passwd**, you can include a test for the 3rd field. Here we're only going after UIDs that are 1000 and above: - -``` -$ awk -F":" ' $3 >= 1000 ' /etc/passwd -nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin -shs:x:1000:1000:Sandra Henry-Stocker,,,:/home/shs:/bin/bash -nemo:x:1001:1001:Nemo,,,:/home/nemo:/usr/bin/zsh -dory:x:1002:1002:Dory,,,:/home/dory:/bin/bash -... -``` - -If you want to add a title for your listing, you can add a BEGIN clause: - -``` -$ awk -F":" 'BEGIN {print "user accounts:"} $3 >= 1000 ' /etc/passwd -user accounts: -nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin -shs:x:1000:1000:Sandra Henry-Stocker,,,:/home/shs:/bin/bash -nemo:x:1001:1001:Nemo,,,:/home/nemo:/usr/bin/zsh -dory:x:1002:1002:Dory,,,:/home/dory:/bin/bash -``` - -If you want more than one line in your title, you can separate your intended output lines with "\n" (newline characters). - -``` -$ awk -F":" 'BEGIN {print "user accounts\n============="} $3 >= 1000 ' /etc/passwd -user accounts -============= -nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin -shs:x:1000:1000:Sandra Henry-Stocker,,,:/home/shs:/bin/bash -nemo:x:1001:1001:Nemo,,,:/home/nemo:/usr/bin/zsh -dory:x:1002:1002:Dory,,,:/home/dory:/bin/bash -``` - -### Doing math with awk - -**awk** provides a surprising mathematical ability and can calculate square roots, logs, tangents, etc. - -Here are a couple examples: - -``` -$ awk 'BEGIN {print sqrt(2019)}' -44.9333 -$ awk 'BEGIN {print log(2019)}' -7.61036 -``` - -For more details on **awk**'s mathematical skills, check out [Doing math with awk][3]. - -### awk scripts - -You can also write standalone scripts with **awk**. Here's an example that mimics one of the examples provided earlier, but also counts the number of users with accounts on the system. - -``` -#!/usr/bin/awk -f - -# This line is a comment - -BEGIN { - printf "%s\n","User accounts:" - print "==============" - FS=":" - n=0 -} - -# Now we'll run through the data -{ - if ($3 >= 1000) { - print $1 - n ++ - } -} - -END { - print "==============" - print n " accounts" -} -``` - -Notice how the BEGIN section, which is run only when the script starts, provides a heading, dictates the field separator and sets up a counter to start with 0. The script also includes an END section which only runs after all the lines in the text provided to the script have been processed. It displays the final count of lines that meet the specification in the middle section (third field is 1,000 or larger) - -A long-standing Unix command, **awk** still provides very useful services and remains one of the reasons that I fell in love with Unix many decades ago. - -To see **awk** in action, click below. - -Join the Network World communities on [Facebook][4] and [LinkedIn][5] to comment on topics that are top of mind. - --------------------------------------------------------------------------------- - -via: https://www.networkworld.com/article/3454979/the-many-faces-of-awk.html - -作者:[Sandra Henry-Stocker][a] -选题:[lujun9972][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/ -[b]: https://github.com/lujun9972 -[1]: https://www.networkworld.com/newsletters/signup.html -[2]: https://www.networkworld.com/article/3440100/take-the-intelligent-route-with-consumption-based-storage.html?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE20773&utm_content=sidebar ( Take the Intelligent Route with Consumption-Based Storage) -[3]: https://www.networkworld.com/article/2974753/doing-math-with-awk.html -[4]: https://www.facebook.com/NetworkWorld/ -[5]: https://www.linkedin.com/company/network-world diff --git a/translated/tech/20191125 The many faces of awk.md b/translated/tech/20191125 The many faces of awk.md new file mode 100644 index 0000000000..7eff1e9883 --- /dev/null +++ b/translated/tech/20191125 The many faces of awk.md @@ -0,0 +1,219 @@ +[#]: collector: (lujun9972) +[#]: translator: (luuming) +[#]: reviewer: ( ) +[#]: publisher: ( ) +[#]: url: ( ) +[#]: subject: (The many faces of awk) +[#]: via: (https://www.networkworld.com/article/3454979/the-many-faces-of-awk.html) +[#]: author: (Sandra Henry-Stocker https://www.networkworld.com/author/Sandra-Henry_Stocker/) + +千面 awk +====== +`awk` 命令不仅提供了简单的输入字符串筛选功能,还包含提取数据列,打印简单文本,筛选内容——甚至做一些数学计算。 + +![Thinkstock][6] + +如果你仅使用 `awk` 选取一行中的特定文本,那么你可能错过了它的很多功能。在这篇文章中,我们会来看看使用 `awk` 可以帮你做一些其他的什么事情,并提供一些例子。 + +### 提取数据列 + +`awk` 所提供的最简单与最常用的功能便是从文件或管道传输的数据中选取特定的内容。默认使用空格当做分隔符,这非常简单。 +``` +$ echo one two three four five | awk ‘{print $4}’ +four +$ who | awk ‘{print $1}’ +jdoe +fhenry +``` + +空格指的是一系列的 `space` 或 `tab`。在下面所展示的命令里,`awk` 从提供的数据中筛选第一和第四项。 + +`awk` 命令也可以通过在其后增加文件名的方式从文本文件中获取数据。 + +``` +$ awk '{print $1,$5,$NF}' HelenKellerQuote +The beautiful heart. +(LCTT 译注:“The best and most beautiful things in the world can not be seen or even touched , they must be felt with heart.” ——海伦凯勒) +``` +在这个例子中,`awk` 挑选了一行中的第一个、第五个和最后一个字段。 + +命令中的`$NF` 指定选取每行的最后一个字段。这是因为`NF`代表一行中的字段数量Number of Field,也就是 23,而 `$NF` 就代表着那个字段的值,也就是`heart`。最后的句号也包含进去了,因为它是最后一个字符串的一部分。 + +字段能以任何有用的形式打印。在这个例子中,我们将字段以日期的格式进行打印输出。 +``` +$ date | awk '{print $4,$3,$2}' +2019 Nov 22 +``` + +如果你省略了 `awk` 命令中字段指示符之间的逗号,输出将会挤成一个字符串。 +``` +$ date | awk '{print $4 $3 $2}' +2019Nov21 +``` + +如果你将通常使用的逗号替换为连字符,`awk` 就会尝试将两个字段的值相减——或许这并不是你想要的。它不会将连字符插入到输出结果中。相反地,它对输出做了一些数学计算。 + +``` +$ date | awk '{print $4-$3-$2}' +1997 +``` + +在这个例子中,它将年“2019”和日期“22”相减,并忽略了中间的“Nov”。 + +如果你想要空格之外的字符作为输出分隔符,你可以通过 `OFS`(输出分隔符output field separator)指定分隔符,就像这样: + +``` +$ date | awk '{OFS="-"; print $4,$3,$2}' +2019-Nov-22 +``` + +### 打印简单文本 + +你也可以使用 `awk` 简单地显示一些文本。当然了,比起 `awk` 你可能更想使用 `echo` 命令。但换句话说,作为 `awk` 脚本的一部分,打印某些相关性文本将会非常实用。这里有一个没什么用的例子: + +``` +$ awk 'BEGIN {print "Hello, World" }' +Hello, World +``` + +下面的例子更加合理,添加一行文本标签来更好的辨识数据。 + +``` +$ who | awk 'BEGIN {print "Current logins:"} {print $1}' +Current logins: +shs +nemo +``` + +### 指定字段分隔符 + +不是所有的输入都以空格作为分隔符的。如果你的文本通过其它的字符作为分隔符(例如:逗号、冒号、分号),你可以通过 `-F` 选项(输入分隔符)告诉 `awk`: + +``` +$ cat testfile +a:b:c,d:e +$ awk -F : '{print $2,$3}' testfile +b c,d +``` + +下面是一个更加有用的例子——从冒号分隔的 `/etc/passwd` 文件中获取数据: + +``` +$ awk -F: '{print $1}' /etc/passwd | head -11 +root +daemon +bin +sys +sync +games +man +lp +mail +news +uucp +``` + +### 筛选内容 + +你也可以使用 `awk` 命令评估字段。例如你仅仅想列出 `/etc/passwd` 中的用户账号,就可以对第三个字段做一些筛选。下面的例子中我们只关注大于等于 1000 的 UID: + +``` +$ awk -F":" ' $3 >= 1000 ' /etc/passwd +nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin +shs:x:1000:1000:Sandra Henry-Stocker,,,:/home/shs:/bin/bash +nemo:x:1001:1001:Nemo,,,:/home/nemo:/usr/bin/zsh +dory:x:1002:1002:Dory,,,:/home/dory:/bin/bash +... +``` + +如果你想为输出增加标题,可以添加 `BEGIN` 从句: + +``` +$ awk -F":" 'BEGIN {print "user accounts:"} $3 >= 1000 ' /etc/passwd +user accounts: +nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin +shs:x:1000:1000:Sandra Henry-Stocker,,,:/home/shs:/bin/bash +nemo:x:1001:1001:Nemo,,,:/home/nemo:/usr/bin/zsh +dory:x:1002:1002:Dory,,,:/home/dory:/bin/bash +``` + +如果你想要不止一行的标题,你可以通过 `"\n"` 分隔输出: + +``` +$ awk -F":" 'BEGIN {print "user accounts\n============="} $3 >= 1000 ' /etc/passwd +user accounts +============= +nobody:x:65534:65534:nobody:/nonexistent:/usr/sbin/nologin +shs:x:1000:1000:Sandra Henry-Stocker,,,:/home/shs:/bin/bash +nemo:x:1001:1001:Nemo,,,:/home/nemo:/usr/bin/zsh +dory:x:1002:1002:Dory,,,:/home/dory:/bin/bash +``` + +### 在 awk 中进行数学计算 + +`awk` 提供了惊人的数学计算能力,并且可以开平方,算 `log`,算 `tan` 等等。 + +这里有一对例子: + +``` +$ awk 'BEGIN {print sqrt(2019)}' +44.9333 +$ awk 'BEGIN {print log(2019)}' +7.61036 +``` + +想要详细了解 `awk` 的数学计算能力,可以看《[使用 awk 进行数学计算][3]》这篇文章。 + +### awk 脚本 + +你也可以使用 `awk` 写一套单独的脚本。下面的例子模仿了之前写过的一个,不过还计算了系统里账户的数量。 + +``` +#!/usr/bin/awk -f + +# 这一行是注释 + +BEGIN { + printf "%s\n","User accounts:" + print "==============" + FS=":" + n=0 +} + +# 现在开始遍历数据 +{ + if ($3 >= 1000) { + print $1 + n ++ + } +} + +END { + print "==============" + print n " accounts" +} +``` + +注意 `BEGIN` 那一节是如何提供标题、指定字段分隔符和初始化计数器的,它仅在脚本初始化时期执行。这个脚本也包含 `END` 节,它仅在中间所有命令处理完成之后运行,显示了所有中间小节所筛选数据的最终行数(第三个字段大于等于 1000)。 + +作为一个长存于 Unix 之上的命令,`awk` 依旧提供着非常有用的服务,这也是我几十年前爱上 Unix 的原因之一。 + +-------------------------------------------------------------------------------- + +via: https://www.networkworld.com/article/3454979/the-many-faces-of-awk.html + +作者:[Sandra Henry-Stocker][a] +选题:[lujun9972][b] +译者:[LuuMing](https://github.com/LuuMing) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://www.networkworld.com/author/Sandra-Henry_Stocker/ +[b]: https://github.com/lujun9972 +[1]: https://www.networkworld.com/newsletters/signup.html +[2]: https://www.networkworld.com/article/3440100/take-the-intelligent-route-with-consumption-based-storage.html?utm_source=IDG&utm_medium=promotions&utm_campaign=HPE20773&utm_content=sidebar ( Take the Intelligent Route with Consumption-Based Storage) +[3]: https://www.networkworld.com/article/2974753/doing-math-with-awk.html +[4]: https://www.facebook.com/NetworkWorld/ +[5]: https://www.linkedin.com/company/network-world +[6]:https://images.techhive.com/images/article/2015/09/thinkstockphotos-512100549-100611755-large.jpg \ No newline at end of file