mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-02-25 00:50:15 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
6e9c31c1b4
234
published/20191104 Fields, records, and variables in awk.md
Normal file
234
published/20191104 Fields, records, and variables in awk.md
Normal file
@ -0,0 +1,234 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (wenwensnow)
|
||||
[#]: reviewer: (wxy)
|
||||
[#]: publisher: (wxy)
|
||||
[#]: url: (https://linux.cn/article-11611-1.html)
|
||||
[#]: subject: (Fields, records, and variables in awk)
|
||||
[#]: via: (https://opensource.com/article/19/11/fields-records-variables-awk)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
awk 中的字段、记录和变量
|
||||
======
|
||||
|
||||
> 这个系列的第二篇,我们会学习字段,记录和一些非常有用的 Awk 变量。
|
||||
|
||||

|
||||
|
||||
Awk 有好几个变种:最早的 `awk`,是 1977 年 AT&T 贝尔实验室所创。它还有一些重构版本,例如 `mawk`、`nawk`。在大多数 Linux 发行版中能见到的,是 GNU awk,也叫 `gawk`。在大多数 Linux 发行版中,`awk` 和 `gawk` 都是指向 GNU awk 的软链接。输入 `awk`,调用的是同一个命令。[GNU awk 用户手册][2]中,能看到 `awk` 和 `gawk` 的全部历史。
|
||||
|
||||
这一系列的[第一篇文章][3] 介绍了 `awk` 命令的基本格式:
|
||||
|
||||
```
|
||||
$ awk [选项] '模式 {动作}' 输入文件
|
||||
```
|
||||
|
||||
`awk` 是一个命令,后面要接选项 (比如用 `-F` 来定义字段分隔符)。想让 `awk` 执行的部分需要写在两个单引号之间,至少在终端中需要这么做。在 `awk` 命令中,为了进一步强调你想要执行的部分,可以用 `-e` 选项来突出显示(但这不是必须的):
|
||||
|
||||
```
|
||||
$ awk -F, -e '{print $2;}' colours.txt
|
||||
yellow
|
||||
blue
|
||||
green
|
||||
[...]
|
||||
```
|
||||
|
||||
### 记录和字段
|
||||
|
||||
`awk` 将输入数据视为一系列*记录*,通常是按行分割的。换句话说,`awk` 将文本中的每一行视作一个记录。每一记录包含多个*字段*。一个字段由*字段分隔符*分隔开来,字段是记录的一部分。
|
||||
|
||||
默认情况下,`awk` 将各种空白符,如空格、制表符、换行符等视为分隔符。值得注意的是,在 `awk` 中,多个*空格*将被视为一个分隔符。所以下面这行文本有两个字段:
|
||||
|
||||
```
|
||||
raspberry red
|
||||
```
|
||||
|
||||
这行也是:
|
||||
|
||||
|
||||
```
|
||||
tuxedo black
|
||||
```
|
||||
|
||||
其他分隔符,在程序中不是这么处理的。假设字段分隔符是逗号,如下所示的记录,就有三个字段。其中一个字段可能会是 0 个字节(假设这一字段中不包含隐藏字符)
|
||||
|
||||
```
|
||||
a,,b
|
||||
```
|
||||
|
||||
### awk 程序
|
||||
|
||||
`awk` 命令的*程序部分*是由一系列规则组成的。通常来说,程序中每个规则占一行(尽管这不是必须的)。每个规则由一个模式,或一个或多个动作组成:
|
||||
|
||||
```
|
||||
模式 { 动作 }
|
||||
```
|
||||
|
||||
在一个规则中,你可以通过定义模式,来确定动作是否会在记录中执行。模式可以是简单的比较条件、正则表达式,甚至两者结合等等。
|
||||
|
||||
这个例子中,程序*只会*显示包含单词 “raspberry” 的记录:
|
||||
|
||||
```
|
||||
$ awk '/raspberry/ { print $0 }' colours.txt
|
||||
raspberry red 99
|
||||
```
|
||||
|
||||
如果没有文本符合模式,该动作将会应用到所有记录上。
|
||||
|
||||
并且,在一条规则只包含模式时,相当于对整个记录执行 `{ print }`,全部打印出来。
|
||||
|
||||
Awk 程序本质上是*数据驱动*的,命令执行结果取决于数据。所以,与其他编程语言中的程序相比,它还是有些区别的。
|
||||
|
||||
### NF 变量
|
||||
|
||||
每个字段都有指定变量,但针对字段和记录,也存在一些特殊变量。`NF` 变量,能存储 `awk` 在当前记录中找到的字段数量。其内容可在屏幕上显示,也可用于测试。下面例子中的数据,来自上篇文章[文本][3]:
|
||||
|
||||
```
|
||||
$ awk '{ print $0 " (" NF ")" }' colours.txt
|
||||
name color amount (3)
|
||||
apple red 4 (3)
|
||||
banana yellow 6 (3)
|
||||
[...]
|
||||
```
|
||||
|
||||
`awk` 的 `print` 函数会接受一系列参数(可以是变量或者字符串),并将它们拼接起来。这就是为什么在这个例子里,每行结尾处,`awk` 会以一个被括号括起来的整数表示字段数量。
|
||||
|
||||
### NR 变量
|
||||
|
||||
另外,除了统计每个记录中的字段数,`awk` 也统计输入记录数。记录数被存储在变量 `NR` 中,它的使用方法和其他变量没有任何区别。例如,为了在每一行开头显示行号:
|
||||
|
||||
```
|
||||
$ awk '{ print NR ": " $0 }' colours.txt
|
||||
1: name color amount
|
||||
2: apple red 4
|
||||
3: banana yellow 6
|
||||
4: raspberry red 3
|
||||
5: grape purple 10
|
||||
[...]
|
||||
```
|
||||
|
||||
注意,写这个命令时可以不在 `print` 后的多个参数间添加空格,尽管这样会降低可读性:
|
||||
|
||||
```
|
||||
$ awk '{print NR": "$0}' colours.txt
|
||||
```
|
||||
|
||||
### printf() 函数
|
||||
|
||||
为了让输出结果时格式更灵活,你可以使用 `awk` 的 `printf()` 函数。 它与 C、Lua、Bash 和其他语言中的 `printf` 相类似。它也接受以逗号分隔的*格式*参数。参数列表需要写在括号里。
|
||||
|
||||
```
|
||||
$ printf 格式, 项目1, 项目2, ...
|
||||
```
|
||||
|
||||
格式这一参数(也叫*格式符*)定义了其他参数如何显示。这一功能是用*格式修饰符*实现的。`%s` 输出字符,`%d` 输出十进制数字。下面的 `printf` 语句,会在括号内显示字段数量:
|
||||
|
||||
```
|
||||
$ awk 'printf "%s (%d)\n",$0,NF}' colours.txt
|
||||
name color amount (3)
|
||||
raspberry red 4 (3)
|
||||
banana yellow 6 (3)
|
||||
[...]
|
||||
```
|
||||
|
||||
在这个例子里,`%s (%d)` 确定了每一行的输出格式,`$0,NF` 定义了插入 `%s` 和 `%d` 位置的数据。注意,和 `print` 函数不同,在没有明确指令时,输出不会转到下一行。出现转义字符 `\n` 时才会换行。
|
||||
|
||||
### Awk 脚本编程
|
||||
|
||||
这篇文章中出现的所有 `awk` 代码,都在 Bash 终端中执行过。面对更复杂的程序,将命令放在文件(*脚本*)中会更容易。`-f FILE` 选项(不要和 `-F` 弄混了,那个选项用于字段分隔符),可用于指明包含可执行程序的文件。
|
||||
|
||||
举个例子,下面是一个简单的 awk 脚本。创建一个名为 `example1.awk` 的文件,包含以下内容:
|
||||
|
||||
```
|
||||
/^a/ {print "A: " $0}
|
||||
/^b/ {print "B: " $0}
|
||||
```
|
||||
|
||||
如果一个文件包含 `awk` 程序,那么在给文件命名时,最好写上 `.awk` 的扩展名。 这样命名不是强制的,但这么做,会给文件管理器、编辑器(和你)一个关于文件内容的很有用的提示。
|
||||
|
||||
执行这一脚本:
|
||||
|
||||
```
|
||||
$ awk -f example1.awk colours.txt
|
||||
A: raspberry red 4
|
||||
B: banana yellow 6
|
||||
A: apple green 8
|
||||
```
|
||||
|
||||
一个包含 `awk` 命令的文件,在最开头一行加上释伴 `#!`,就能变成可执行脚本。创建一个名为 `example2.awk` 的文件,包含以下内容:
|
||||
|
||||
```
|
||||
#!/usr/bin/awk -f
|
||||
#
|
||||
# 除了第一行,在其他行前显示行号
|
||||
#
|
||||
|
||||
NR > 1 {
|
||||
printf "%d: %s\n",NR,$0
|
||||
}
|
||||
```
|
||||
|
||||
可以说,脚本中只有一行,大多数情况下没什么用。但在某些情况下,执行一个脚本,比记住,然后打一条命令要容易的多。一个脚本文件,也提供了一个记录命令具体作用的好机会。以 `#` 号开头的行是注释,`awk` 会忽略它们。
|
||||
|
||||
给文件可执行权限:
|
||||
|
||||
```
|
||||
$ chmod u+x example2.awk
|
||||
```
|
||||
|
||||
执行脚本:
|
||||
|
||||
```
|
||||
$ ./example2.awk colours.txt
|
||||
2: apple red 4
|
||||
2: banana yellow 6
|
||||
4: raspberry red 3
|
||||
5: grape purple 10
|
||||
[...]
|
||||
```
|
||||
|
||||
将 `awk` 命令放在脚本文件中,有一个好处就是,修改和格式化输出会更容易。在终端中,如果能用一行执行多条 `awk` 命令,那么输入多行,才能达到同样效果,就显得有些多余了。
|
||||
|
||||
### 试一试
|
||||
|
||||
你现在已经足够了解,`awk` 是如何执行指令的了。现在你应该能编写复杂的 `awk` 程序了。试着编写一个 awk 脚本,它需要: 至少包括一个条件模式,以及多个规则。如果你想使用除 `print` 和 `printf` 以外的函数,可以参考在线 [gawk 手册][4]。
|
||||
|
||||
下面这个例子是个很好的切入点:
|
||||
|
||||
```
|
||||
#!/usr/bin/awk -f
|
||||
#
|
||||
# 显示所有记录 除了出现以下情况
|
||||
# 如果第一个记录 包含 “raspberry”
|
||||
# 将 “red” 替换成 “pi”
|
||||
|
||||
$1 == "raspberry" {
|
||||
gsub(/red/,"pi")
|
||||
}
|
||||
|
||||
{ print }
|
||||
```
|
||||
|
||||
试着执行这个脚本,看看输出是什么。接下来就看你自己的了。
|
||||
|
||||
这一系列的下一篇文章,将会介绍更多,能在更复杂(更有用!) 脚本中使用的函数。
|
||||
|
||||
这篇文章改编自 [Hacker Public Radio][5] 系列,一个技术社区博客。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/11/fields-records-variables-awk
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[wenwensnow](https://github.com/wenwensnow)
|
||||
校对:[wxy](https://github.com/wxy)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_laptop_code_programming_mountain_view.jpg?itok=yx5buqkr (Man at laptop on a mountain)
|
||||
[2]: https://www.gnu.org/software/gawk/manual/html_node/History.html#History
|
||||
[3]: https://linux.cn/article-11543-1.html
|
||||
[4]: https://www.gnu.org/software/gawk/manual/
|
||||
[5]: http://hackerpublicradio.org/eps.php?id=2129
|
@ -1,226 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to use pkgsrc on Linux)
|
||||
[#]: via: (https://opensource.com/article/19/11/pkgsrc-netbsd-linux)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
How to use pkgsrc on Linux
|
||||
======
|
||||
NetBSD's package manager is generic, flexible, and easy. Here's how to
|
||||
use it.
|
||||
![A person programming][1]
|
||||
|
||||
NetBSD is famous for running on basically anything, but did you know its _second_ claim to fame is the **[pkgsrc][2]** package manager? Like NetBSD, pkgsrc runs on basically anything, or at least anything Unix and Unix-like. You can install pkgsrc on BSD, Linux, Illumos, Solaris, and Mac. All told, over 20 operating systems are supported.
|
||||
|
||||
### Why use pkgsrc?
|
||||
|
||||
With the exception of MacOS, all Unix operating systems ship with a package manager included. You don't necessarily _need_ pkgsrc, but here are three great reasons you may want to try it:
|
||||
|
||||
* **Packaging.** If you're curious about packaging but have yet to try creating a package yourself, pkgsrc is a relatively simple system to use, especially if you're already familiar with Makefiles and build systems like [GNU Autotools][3].
|
||||
* **Generic.** If you use multiple operating systems or distributions, then you probably encounter a package manager for each system. You can use pkgsrc across disparate systems so that when you package an application for one, you've packaged it for all of them.
|
||||
* **Flexible.** In many packaging systems, it's not always obvious how to choose a binary package or a source package. With pkgsrc, the distinction is clear, both methods of installing are equally as easy, and both resolve dependencies for you.
|
||||
|
||||
|
||||
|
||||
### How to install pkgsrc
|
||||
|
||||
Whether you're on BSD, Linux, Illumos, Solaris, or MacOS, the installation process is basically the same:
|
||||
|
||||
1. Use CVS to check out the pkgsrc tree
|
||||
2. Bootstrap the pkgsrc system
|
||||
3. Install packages
|
||||
|
||||
|
||||
|
||||
#### Use CVS to check out the pkgsrc tree
|
||||
|
||||
Before Git, before Subversion, there was **[CVS][4]**. You don't have to know much about CVS to do a checkout of its code—if you're used to Git, then think of _checkout_ as _clone_. When you perform a CVS checkout of pkgsrc, you're downloading "recipes" detailing how each package is to be built. It's a lot of files, but they're small because you're not actually pulling the source code for each package, just the build infrastructure and Makefiles required to build on it demand. Using CVS makes it easy for you to update your pkgsrc checkout when a new one is released.
|
||||
|
||||
The pkgsrc docs recommend keeping your tree in the **/usr** directory, so you must use **sudo** (or become root) to use this command:
|
||||
|
||||
|
||||
```
|
||||
$ cd /usr
|
||||
$ sudo cvs -q -z2 \
|
||||
-d [anoncvs@anoncvs.NetBSD.org][5]:/cvsroot \
|
||||
checkout -r pkgsrc-2019Q3 -P pkgsrc
|
||||
```
|
||||
|
||||
As I'm writing, the latest release is 2019Q3. Check the news section of [pkgsrc.org][6]'s homepage or the [NetBSD documentation][7] to determine the latest release version.
|
||||
|
||||
#### Bootstrap pkgsrc
|
||||
|
||||
After the pkgsrc tree has copied to your computer, you have a **/usr/pkgsrc** directory filled with build scripts. Before you can use them, you must bootstrap pkgsrc so that you have easy access to the relevant commands you need to build and install the software.
|
||||
|
||||
The way you bootstrap **pkgsrc** depends on the OS you're on.
|
||||
|
||||
For NetBSD, you can just use the bundled bootstrapper:
|
||||
|
||||
|
||||
```
|
||||
# cd pkgsrc/bootstrap
|
||||
# ./bootstrap
|
||||
```
|
||||
|
||||
On other systems, there are better ways with some customized features included, provided by Joyent. To find out the exact command to run, visit [pkgsrc.joyent.com][8]. For example, on Linux (Fedora, Debian, Slackware, and so on):
|
||||
|
||||
|
||||
```
|
||||
$ curl -O \
|
||||
<https://pkgsrc.joyent.com/packages/Linux/el7/bootstrap/bootstrap-trunk-x86\_64-20170127.tar.gz>
|
||||
$ BOOTSTRAP_SHA="eb0d6911489579ca893f67f8a528ecd02137d43a"
|
||||
```
|
||||
|
||||
Even though the path suggests that the included files are for RHEL 7, the binaries tend to be compatible with all but the most cutting-edge Linux distributions. And should you find a binary incompatible with the distribution you're on, you have the option to build from source.
|
||||
|
||||
Verify the SHA1 checksum:
|
||||
|
||||
|
||||
```
|
||||
$ echo "${BOOTSTRAP_SHA}" bootstrap-trunk*gz > check-shasum
|
||||
sha1sum -c check-shasum
|
||||
```
|
||||
|
||||
You can also verify the PGP signature:
|
||||
|
||||
|
||||
```
|
||||
$ curl -O \
|
||||
<https://pkgsrc.joyent.com/packages/Linux/el7/bootstrap/bootstrap-trunk-x86\_64-20170127.tar.gz.asc>
|
||||
curl -sS <https://pkgsrc.joyent.com/pgp/56AAACAF.asc> | gpg --import
|
||||
gpg --verify ${BOOTSTRAP_TAR}{.asc,}
|
||||
```
|
||||
|
||||
Once you're confident that you have the right bootstrap kit, install it to **/usr/pkg**:
|
||||
|
||||
|
||||
```
|
||||
`sudo tar -zxpf ${BOOTSTRAP_TAR} -C /`
|
||||
```
|
||||
|
||||
This provides you with the usual pkgsrc commands. Add these locations to [your PATH][9]:
|
||||
|
||||
|
||||
```
|
||||
$ echo "PATH=/usr/pkg/sbin:/usr/pkg/bin:$PATH" >> ~/.bashrc
|
||||
$ echo "MANPATH=/usr/pkg/man:$MANPATH" >> ~/.bashrc
|
||||
```
|
||||
|
||||
If you'd rather use pkgsrc without relying on Joyent's builds, you can just run the **bootstrap** script you got with the pkgsrc tree. Read the relevant README file in the **bootstrap** directory before running it for important system-specific notes.
|
||||
|
||||
![Bootstrapping pkgsrc on NetBSD][10]
|
||||
|
||||
### How to install software with pkgsrc
|
||||
|
||||
Installing a precompiled binary (as you would with DNF or Apt) with pkgsrc is easy. The command for binary installs is **pgkin**, which has its own dedicated site at [pkgin.net][11]. The process ought to feel pretty familiar to anyone who's used Linux.
|
||||
|
||||
To search for the **tmux** package:
|
||||
|
||||
|
||||
```
|
||||
`$ pkgin search tmux`
|
||||
```
|
||||
|
||||
To install the tmux package:
|
||||
|
||||
|
||||
```
|
||||
`$ sudo pkgin install tmux`
|
||||
```
|
||||
|
||||
The **pkgin** command's aim is to mimic the behavior of typical Linux package managers, so there are options to list available packages, to query available packages to find what provides a specific executable, and so on.
|
||||
|
||||
### How to build from source code with pkgsrc
|
||||
|
||||
The real power of pkgsrc, though, is the ease of building a package from source. You downloaded all 20,000+ build scripts in the first setup step, and you can access those by navigating into your pkgsrc tree directly.
|
||||
|
||||
For example, to build **tcsh** from source, first, locate the build script:
|
||||
|
||||
|
||||
```
|
||||
$ find /usr/pkgsrc -type d -name "tcsh"
|
||||
/usr/pkgsrc/shells/tcsh
|
||||
```
|
||||
|
||||
Next, change into the source directory:
|
||||
|
||||
|
||||
```
|
||||
`$ cd /usr/pgksrc/shells/tcsh`
|
||||
```
|
||||
|
||||
The build script directory contains a number of files to help the application build on your system, but notably, it contains the **DESCR** file, which contains a description of the software, as well as the **Makefile** that triggers the build.
|
||||
|
||||
|
||||
```
|
||||
$ ls
|
||||
CVS DESCR Makefile
|
||||
PLIST distinfo patches
|
||||
$ cat DESCR
|
||||
TCSH is an extended C-shell with many useful features like
|
||||
filename completion, history editing, etc.
|
||||
$
|
||||
```
|
||||
|
||||
When you're ready, build, and install:
|
||||
|
||||
|
||||
```
|
||||
`$ sudo bmake install`
|
||||
```
|
||||
|
||||
The pkgsrc system uses the **bmake** command (provided by the pkgsrc checkout in the first step), so be sure to use **bmake** (and not **make** out of habit).
|
||||
|
||||
If you're building for several systems, you can create a package instead of installing right away:
|
||||
|
||||
|
||||
```
|
||||
$ cd /usr/pgksrc/shells/tcsh
|
||||
$ sudo bmake package
|
||||
[...]
|
||||
=> Creating binary package in /usr/pkgsrc/packages/All/tcsh-X.Y.Z.tgz
|
||||
```
|
||||
|
||||
The packages that pkgsrc creates are standard tarballs, but they can be installed conveniently with **pkg_add**:
|
||||
|
||||
|
||||
```
|
||||
$ sudo pkg_add /usr/pkgsrc/packages/All/tcsh-X.Y.Z.tgz
|
||||
tcsh-X.Y.Z: adding /usr/pkg/bin/tcsh to /etc/shells
|
||||
$ tcsh
|
||||
localhost%
|
||||
```
|
||||
|
||||
The **pkgtools** collection from pkgsrc provides the **pkg_add**, **pkg_info**, **pkg_admin**, **pkg_create**, and **pkg_delete** commands to help manage packages you build and maintain on your system.
|
||||
|
||||
### Pkgsrc for easy management
|
||||
|
||||
The pkgsrc system offers a direct, hands-on approach to package management. If you're looking for a package manager that stays out of your way and invites customization, give pkgsrc a try on whatever Unix or Unix-like OS you're running.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/11/pkgsrc-netbsd-linux
|
||||
|
||||
作者:[Seth Kenlon][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://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_keyboard_laptop_development_code_woman.png?itok=vbYz6jjb (A person programming)
|
||||
[2]: http://pkgsrc.org
|
||||
[3]: https://opensource.com/article/19/7/introduction-gnu-autotools
|
||||
[4]: http://www.netbsd.org/developers/cvs-repos/cvs_intro.html#intro
|
||||
[5]: mailto:anoncvs@anoncvs.NetBSD.org
|
||||
[6]: http://pkgsrc.org/
|
||||
[7]: http://www.netbsd.org/docs/pkgsrc/getting.html
|
||||
[8]: http://pkgsrc.joyent.com/
|
||||
[9]: https://opensource.com/article/17/6/set-path-linux
|
||||
[10]: https://opensource.com/sites/default/files/uploads/pkgsrc-bootstrap.jpg (Bootstrapping pkgsrc on NetBSD)
|
||||
[11]: http://pkgin.net
|
@ -1,5 +1,5 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: ( )
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
|
@ -1,249 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (liwenwensnow)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (Fields, records, and variables in awk)
|
||||
[#]: via: (https://opensource.com/article/19/11/fields-records-variables-awk)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
awk中的字段,记录和变量
|
||||
======
|
||||
这个系列的第二篇,我们会学习 字段,记录和一些非常有用的awk变量。
|
||||
![Man at laptop on a mountain][1]
|
||||
|
||||
Awk 有好几个变种: 最早的 **awk**, 是1977 年 AT&T Bell 实验室所创。它还有一些重构版本,例如 **mawk**, **nawk**。在大多数Linux 发行版中能见到的,是 GNU awk,也叫**gawk**。 在大多数 Linux 发行版中,awk 和 gawk 都是指向 GNU awk 的链接。 输入awk命令,也是同样的效果。 [GNU awk 用户手册][2]中,能看到 awk 和 gawk 的全部历史。
|
||||
|
||||
这一系列的[第一篇文章][3] 介绍了awk 命令的基本格式:
|
||||
|
||||
```
|
||||
`$ awk [options] 'pattern {action}' inputfile`
|
||||
```
|
||||
|
||||
Awk 是一个命令,后面要接选项 (比如用 **-F** 来定义字段分隔符)。 想让awk 执行的部分需要写在 两个单引号之间,至少在终端中需要这么做。 在awk 命令中,为了进一步强调你想要执行的部分,可以用 **-e** 选项来突出显示 (但这不是必须的):
|
||||
|
||||
|
||||
```
|
||||
$ awk -F, -e '{print $2;}' colours.txt
|
||||
yellow
|
||||
blue
|
||||
green
|
||||
[...]
|
||||
```
|
||||
|
||||
### 记录和字段
|
||||
|
||||
Awk 将输入数据视为 一系列 _记录_ , 通常是按行分割的。 换句话说,awk 将文本中的每一行视作一个记录。每一记录包含多个 _字段_. 一个字段由 _字段分隔符_ 分隔开来,字段是记录的一部分.
|
||||
|
||||
默认情况下,awk 将各种空白符,如空格,tab,换行符等视为分隔符。 值得注意的是,在awk 中,多个 _空格_ 将被视为一个分隔符。所以下面这行文本有两个字段:
|
||||
|
||||
|
||||
```
|
||||
`raspberry red`
|
||||
```
|
||||
|
||||
这行也是:
|
||||
|
||||
|
||||
```
|
||||
`tuxedo black`
|
||||
```
|
||||
|
||||
其他分隔符,在程序中不是这么处理的。假设字段分隔符是逗号,如下所示的记录,就有三个字段。其中一个字段可能会是0个字节(假设这一字段中不包含隐藏字符)
|
||||
|
||||
```
|
||||
`a,,b`
|
||||
```
|
||||
|
||||
### awk 程序
|
||||
|
||||
awk 命令的 _程序部分_ 是由一系列规则组成的。通常来说,程序中每个规则占一行(尽管这不是必须的)。 每个规则由一个模式,或一个/多个动作组成:
|
||||
|
||||
```
|
||||
`pattern { action }`
|
||||
```
|
||||
|
||||
在一个规则中,你可以通过定义模式,来确定行动是否会在记录中执行。 模式可以是简单的比较条件,正则表达式,甚至两者结合等等。
|
||||
|
||||
这个例子中,程序 _只会_ 显示包含 单词 “raspberry” 的记录:
|
||||
|
||||
|
||||
```
|
||||
$ awk '/raspberry/ { print $0 }' colours.txt
|
||||
raspberry red 99
|
||||
```
|
||||
|
||||
如果没有文本符合模式,最终结果会对应所有记录。
|
||||
|
||||
并且,在一条规则只包含一个模式时,相当于对整个记录执行 **{ print }** 。
|
||||
|
||||
Awk 程序本质上是 _数据驱动_ 的,命令执行结果取决于数据。所以,与其他编程语言中的程序相比,它还是有些区别的。
|
||||
|
||||
### NF 变量
|
||||
|
||||
每个字段都有指定变量,但针对字段和记录,也存在一些特殊变量。 **NF** 变量,能存储awk在当前记录中找到的数字字段。其内容可在屏幕上显示,也可用于测试。 下面例子中的数据,来自上篇文章[文本][3]:
|
||||
|
||||
|
||||
```
|
||||
$ awk '{ print $0 " (" NF ")" }' colours.txt
|
||||
name color amount (3)
|
||||
apple red 4 (3)
|
||||
banana yellow 6 (3)
|
||||
[...]
|
||||
```
|
||||
|
||||
Awk 的 **print** 函数会接受一系列参数(可以是变量或者字符),并将它们拼接起来。这就是为什么在这个例子里,每行结尾处,awk 会显示一个被括号括起来的整数。
|
||||
|
||||
### NR 变量
|
||||
|
||||
另外,为了计算每个记录中的字段数,awk 也计算输入记录。 记录数目被存储在变量 **NR** 中,它的使用方法和其他变量没有任何区别。例如,为了在每一行开头显示行号:
|
||||
|
||||
```
|
||||
$ awk '{ print NR ": " $0 }' colours.txt
|
||||
1: name color amount
|
||||
2: apple red 4
|
||||
3: banana yellow 6
|
||||
4: raspberry red 3
|
||||
5: grape purple 10
|
||||
[...]
|
||||
```
|
||||
|
||||
注意,在这个命令下输入数据时,可以不遵循在 **print** 后的规则,参数间可以不写空格,尽管这样会降低可读性:
|
||||
|
||||
|
||||
```
|
||||
`$ awk '{print NR": "$0}' colours.txt`
|
||||
```
|
||||
|
||||
### printf() 函数
|
||||
|
||||
为了输出结果时格式更灵活,你可以使用 awk 的 **printf()** 函数。 它与C,Lua,Bash和其他语言中的 **printf** 相类似。 它也接受 _格式_ ,加逗号分隔的参数。参数列表需要写在括号里。
|
||||
|
||||
|
||||
```
|
||||
`$ printf format, item1, item2, ...`
|
||||
```
|
||||
|
||||
格式这一参数(也叫 _格式符_ ) 定义了其他参数如何显示。 这一功能是用 _格式修饰符_ 来实现的。 **%s** 显示字符, **%d** 显示数字。 下面的**printf** 语句,会在括号内显示字段数量:
|
||||
|
||||
```
|
||||
$ awk 'printf "%s (%d)\n",$0,NF}' colours.txt
|
||||
name color amount (3)
|
||||
raspberry red 4 (3)
|
||||
banana yellow 6 (3)
|
||||
[...]
|
||||
```
|
||||
|
||||
|
||||
在这个例子里, **%s (%d)** 确定了每一行的输出格式,**$0,NF** 定义了插入 **%s** 和 **%d** 位置的数据。注意,和**print** 函数不同,在没有明确指令时,输出不会转到下一行。出现 转义字符 **\n** 时才会换行。
|
||||
|
||||
### Awk 脚本编程
|
||||
|
||||
这篇文章中出现的所有awk代码,都在Bash终端中执行过。 面对更复杂的程序,将命令放在文件( _脚本_ )中会更容易。 **-f FILE** 选项(不要和 **-F** 弄混了,那个选项用于字段分隔符),可用于指明包含可执行程序的文件。
|
||||
|
||||
举个例子,下面是一个简单的awk 脚本。 创建一个名为 **example1.awk** 的文件,包含以下内容:
|
||||
|
||||
|
||||
```
|
||||
/^a/ {print "A: " $0}
|
||||
/^b/ {print "B: " $0}
|
||||
```
|
||||
|
||||
如果一个文件包含 awk 程序,那么在给文件命名时,最好写上 **.awk** 的扩展名。 这样命名不是强制的,但这么做,会给文件管理器,编辑者(和你)一个关于文件内容的,很有用的提示。
|
||||
|
||||
执行这一脚本:
|
||||
|
||||
|
||||
```
|
||||
$ awk -f example1.awk colours.txt
|
||||
A: raspberry red 4
|
||||
B: banana yellow 6
|
||||
A: apple green 8
|
||||
```
|
||||
|
||||
一个包含 awk 命令的文件,在最开头一行加上 **#!** ,就能变成可执行脚本。 创建一个名为 **example2.awk** 的文件,包含以下内容:
|
||||
|
||||
```
|
||||
#!/usr/bin/awk -f
|
||||
#
|
||||
# 除了第一行,在其他行前显示行号
|
||||
#
|
||||
|
||||
NR > 1 {
|
||||
printf "%d: %s\n",NR,$0
|
||||
}
|
||||
```
|
||||
|
||||
可以说,脚本中只有一行,大多数情况下没什么用。 但在某些情况下,执行一个脚本,比记住,然后打一条命令要容易的多。 一个脚本文件,也提供了一个记录命令具体作用的好机会。 以 **#** 号开头的行是注释,awk 会忽略它们。
|
||||
|
||||
给文件可执行权限:
|
||||
|
||||
|
||||
```
|
||||
`$ chmod u+x example2.awk`
|
||||
```
|
||||
|
||||
执行脚本:
|
||||
|
||||
|
||||
```
|
||||
$ ./example2.awk colours.txt
|
||||
2: apple red 4
|
||||
2: banana yellow 6
|
||||
4: raspberry red 3
|
||||
5: grape purple 10
|
||||
[...]
|
||||
```
|
||||
|
||||
|
||||
将awk 命令放在脚本文件中,有一个好处就是,修改和格式化输出会更容易。在终端中,如果能用一行执行多条awk命令,那么输入多行,才能达到同样效果,就显得有些多余了。
|
||||
|
||||
### 试一试
|
||||
|
||||
你现在已经足够了解, awk 是如何执行指令的了。现在你应该能编写复杂的awk 程序了。 试着编写一个awk 脚本,它需要: 至少包括一个条件模式,以及多个规则。如果你想使用除 **print** 和 **printf** 以外的函数,可以参考在线[ gawk 手册][4] .
|
||||
|
||||
|
||||
下面这个例子是个很好的切入点:
|
||||
|
||||
|
||||
```
|
||||
#!/usr/bin/awk -f
|
||||
#
|
||||
# 显示所有记录 除了出现以下情况
|
||||
# 如果第一个记录 包含 “raspberry”
|
||||
# 将 “red” 替换成 “pi”
|
||||
|
||||
$1 == "raspberry" {
|
||||
gsub(/red/,"pi")
|
||||
}
|
||||
|
||||
{ print }
|
||||
```
|
||||
|
||||
试着执行这个脚本,看看输出是什么。接下来就看你自己的了。
|
||||
|
||||
|
||||
这一系列的下一篇文章,将会介绍更多,能在更复杂(更有用!) 脚本中使用的函数。
|
||||
|
||||
* * *
|
||||
|
||||
_这篇文章改编自 [Hacker Public Radio][5] 系列,一个技术社区博客_
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/11/fields-records-variables-awk
|
||||
|
||||
作者:[Seth Kenlon][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://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_laptop_code_programming_mountain_view.jpg?itok=yx5buqkr (Man at laptop on a mountain)
|
||||
[2]: https://www.gnu.org/software/gawk/manual/html_node/History.html#History
|
||||
[3]: https://opensource.com/article/19/10/intro-awk
|
||||
[4]: https://www.gnu.org/software/gawk/manual/
|
||||
[5]: http://hackerpublicradio.org/eps.php?id=2129
|
221
translated/tech/20191119 How to use pkgsrc on Linux.md
Normal file
221
translated/tech/20191119 How to use pkgsrc on Linux.md
Normal file
@ -0,0 +1,221 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (geekpi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to use pkgsrc on Linux)
|
||||
[#]: via: (https://opensource.com/article/19/11/pkgsrc-netbsd-linux)
|
||||
[#]: author: (Seth Kenlon https://opensource.com/users/seth)
|
||||
|
||||
如何在 Linux 上使用 pkgsrc
|
||||
======
|
||||
NetBSD 的软件包管理器通用、灵活又容易。下面是如何使用它。
|
||||
![A person programming][1]
|
||||
|
||||
NetBSD 以能在几乎所有平台上运行而闻名,但你知道它_第二_有名的 **[pkgsrc][2]** 包管理器吗?像 NetBSD 一样,pkgsrc 基本上可以在任何系统上运行,或者至少在任意 Unix 和类 Unix 的系统上上运行。你可以在 BSD、Linux、Illumos、Solaris 和 Mac 上安装 pkgsrc。它总共支持 20 多种操作系统。
|
||||
|
||||
### 为什么使用 pkgsrc?
|
||||
|
||||
除了 MacOS 之外,所有 Unix 操作系统均自带包管理器。你不一定 _需要_ pkgsrc,但这可能是你想尝试的三个重要原因:
|
||||
|
||||
* **打包**。如果你对打包感到好奇,但尚未尝试自己创建一个软件包,那么 pkgsrc 是一个相对简单的系统,尤其是如果你已经熟悉 Makefile 和类似 [GNU Autotools][3] 之类的构建系统时。
|
||||
* **通用**。如果你使用多个操作系统或发行版,那么可能会遇到每个系统的包管理器。你可以在不同的系统上使用 pkgsrc,以便在一个系统中打包了程序,就为所有系统打包了该程序。
|
||||
* **灵活**。在许多打包系统中,如何选择二进制包或源码包并不总是很明显。使用 pkgsrc,区别很明显,两种安装方法都一样容易,并且都可以为你解决依赖关系。
|
||||
|
||||
|
||||
|
||||
### 如何安装 pkgsrc
|
||||
|
||||
无论你使用的是 BSD、Linux、Illumos、Solaris 还是 MacOS,安装过程都基本相同:
|
||||
|
||||
1. 使用 CVS 检出 pkgsrc 树
|
||||
2. 引导 pkgsrc 系统
|
||||
3. 安装软件包
|
||||
|
||||
|
||||
|
||||
#### 使用 CVS 检出 pkgsrc 树
|
||||
|
||||
在 Git 和 Subversion 之前,就有了 **[CVS][4]**。要检出代码你无需了解 CVS 太多,如果你习惯 Git,那么可以将_检出_ (checkout) 称为 _克隆_ (clone)。当你用 CVS 检出 pkgsrc 时,你就在下载详细说明如何构建每个软件包的“配方”(“recipes”)。它有很多文件,但是它们都很小,因为你实际上并没有拉取每个包的源码,而只有按需构建时需要的构建基础架构和 Makefile。使用 CVS,你可以轻松地在新版本发布时更新 pkgsrc 检出。
|
||||
|
||||
pkgsrc 文档建议将树放在 **/usr** 目录下,因此你必须使用 **sudo** (或成为 root)运行此命令:
|
||||
|
||||
|
||||
```
|
||||
$ cd /usr
|
||||
$ sudo cvs -q -z2 \
|
||||
-d [anoncvs@anoncvs.NetBSD.org][5]:/cvsroot \
|
||||
checkout -r pkgsrc-2019Q3 -P pkgsrc
|
||||
```
|
||||
|
||||
在我撰写本文时,最新版本是 2019Q3。请检查 [pkgsrc.org][6] 主页的新闻部分或 [NetBSD文档][7],以确定最新版本。
|
||||
|
||||
#### 引导 pkgsrc
|
||||
|
||||
pkgsrc 树复制到你的计算机后,你会看到一个充满构建脚本的 **/usr/pkgsrc** 目录。在使用之前,你必须引导 pkgsrc,以便你可以轻松地访问构建和安装软件所需的相关命令。
|
||||
|
||||
引导 **pkgsrc** 的方式取决于你所使用操作系统。
|
||||
|
||||
对于 NetBSD,你只需使用捆绑的引导器:
|
||||
|
||||
|
||||
```
|
||||
# cd pkgsrc/bootstrap
|
||||
# ./bootstrap
|
||||
```
|
||||
|
||||
在其他系统上,还有更好的方法,包括一些自定义功能,它由 Joyent 提供。要了解运行的确切命令,请访问 [pkgsrc.joyent.com][8]。比如,在 Linux(Fedora、Debian、Slackware 等)上:
|
||||
|
||||
```
|
||||
$ curl -O \
|
||||
<https://pkgsrc.joyent.com/packages/Linux/el7/bootstrap/bootstrap-trunk-x86\_64-20170127.tar.gz>
|
||||
$ BOOTSTRAP_SHA="eb0d6911489579ca893f67f8a528ecd02137d43a"
|
||||
```
|
||||
|
||||
尽管路径暗示文件适用于 RHEL 7,但二进制文件往往与所有(最前沿的 Linux 发行版)兼容。如果你发现二进制文件与你的发行版不兼容,你可以选择从源码构建。
|
||||
|
||||
验证 SHA1 校验和:
|
||||
|
||||
|
||||
```
|
||||
$ echo "${BOOTSTRAP_SHA}" bootstrap-trunk*gz > check-shasum
|
||||
sha1sum -c check-shasum
|
||||
```
|
||||
|
||||
你还可以验证 PGP 签名:
|
||||
|
||||
|
||||
```
|
||||
$ curl -O \
|
||||
<https://pkgsrc.joyent.com/packages/Linux/el7/bootstrap/bootstrap-trunk-x86\_64-20170127.tar.gz.asc>
|
||||
curl -sS <https://pkgsrc.joyent.com/pgp/56AAACAF.asc> | gpg --import
|
||||
gpg --verify ${BOOTSTRAP_TAR}{.asc,}
|
||||
```
|
||||
|
||||
当你确认你已有正确的引导套件,将其安装到 **/usr/pkg**:
|
||||
|
||||
|
||||
```
|
||||
`sudo tar -zxpf ${BOOTSTRAP_TAR} -C /`
|
||||
```
|
||||
|
||||
它为你提供了通常的 pkgsrc 命令。将这些位置添加到[你的 PATH 环境变量中][9]:
|
||||
|
||||
|
||||
```
|
||||
$ echo "PATH=/usr/pkg/sbin:/usr/pkg/bin:$PATH" >> ~/.bashrc
|
||||
$ echo "MANPATH=/usr/pkg/man:$MANPATH" >> ~/.bashrc
|
||||
```
|
||||
|
||||
如果你宁愿使用 pkgsrc 而不依赖于 Joyent 的构建,那么只需运行 pkgsrc 树的**引导**脚本即可。在运行特定于系统的脚本之前,请先阅读 **bootstrap** 目录中相关 README 文件。
|
||||
|
||||
![Bootstrapping pkgsrc on NetBSD][10]
|
||||
|
||||
### 如何使用 pkgsrc 安装软件
|
||||
|
||||
使用 pkgsrc 安装预编译的二进制文件(就像使用 DNF 或 Apt 一样)是很容易的。二进制安装的命令是 **pgkin**,它有自己的专门网站 [pkgin.net][11]。对于任何用过 Linux 的人来说,这个过程应该感觉相当熟悉。
|
||||
|
||||
要搜索 **tmux** 包:
|
||||
|
||||
```
|
||||
`$ pkgin search tmux`
|
||||
```
|
||||
|
||||
要安装 tmux 包:
|
||||
|
||||
|
||||
```
|
||||
`$ sudo pkgin install tmux`
|
||||
```
|
||||
|
||||
**pkgin** 命令的目的是模仿典型的 Linux 包管理器的行为,因此有选项可以列出可用的包、查找包提供的特定可执行文件,等等。
|
||||
|
||||
### 如何使用 pkgsrc 从源码构建
|
||||
|
||||
然而,pkgsrc 真正强大的地方是方便地从源码构建包。你在第一步中检出了所有 20000 多个构建脚本,你可以直接进入 pkgsrc 树来访问这些脚本。
|
||||
|
||||
例如,要从源码构建 **tcsh**,首先找到构建脚本:
|
||||
|
||||
|
||||
```
|
||||
$ find /usr/pkgsrc -type d -name "tcsh"
|
||||
/usr/pkgsrc/shells/tcsh
|
||||
```
|
||||
|
||||
接下来,进入源码目录:
|
||||
|
||||
|
||||
```
|
||||
`$ cd /usr/pgksrc/shells/tcsh`
|
||||
```
|
||||
|
||||
构建脚本目录包含许多文件来帮助在你的系统上构建应用,但值得注意的是,这里面有 **DESCR** 文件,它包含软件说明,以及触发构建的 **Makefile**。
|
||||
|
||||
```
|
||||
$ ls
|
||||
CVS DESCR Makefile
|
||||
PLIST distinfo patches
|
||||
$ cat DESCR
|
||||
TCSH is an extended C-shell with many useful features like
|
||||
filename completion, history editing, etc.
|
||||
$
|
||||
```
|
||||
|
||||
准备就绪后,构建并安装:
|
||||
|
||||
|
||||
```
|
||||
`$ sudo bmake install`
|
||||
```
|
||||
|
||||
pkgsrc 系统使用 **bmake** 命令(在第一步检出 pkgsrc 后提供),因此请务必使用 **bmake**(而不是出于习惯使用 **make**)。
|
||||
|
||||
如果要为多个系统构建,那么你可以创建一个包,而不是立即安装:
|
||||
|
||||
|
||||
```
|
||||
$ cd /usr/pgksrc/shells/tcsh
|
||||
$ sudo bmake package
|
||||
[...]
|
||||
=> Creating binary package in /usr/pkgsrc/packages/All/tcsh-X.Y.Z.tgz
|
||||
```
|
||||
|
||||
pkgsrc 创建的包是标准的 tarball,但它可以方便地通过 **pkg_add** 安装:
|
||||
|
||||
```
|
||||
$ sudo pkg_add /usr/pkgsrc/packages/All/tcsh-X.Y.Z.tgz
|
||||
tcsh-X.Y.Z: adding /usr/pkg/bin/tcsh to /etc/shells
|
||||
$ tcsh
|
||||
localhost%
|
||||
```
|
||||
|
||||
pkgsrc 的 **pkgtools** 集合提供 **pkg_add**、**pkg_info**、**pkg_admin**、**pkg_create** 和 **pkg_delete** 命令,来帮助管理你在系统上构建和维护软件包。
|
||||
|
||||
### Pkgsrc,易于管理
|
||||
|
||||
pkgsrc 系统提供了直接,容易上手的软件包管理方法。 如果你正在寻找一个不妨碍你并且可以定制的包管理器,请在任何运行 Unix 或类 Unix 的系统上试试 pkgsrc。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/11/pkgsrc-netbsd-linux
|
||||
|
||||
作者:[Seth Kenlon][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/seth
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/computer_keyboard_laptop_development_code_woman.png?itok=vbYz6jjb (A person programming)
|
||||
[2]: http://pkgsrc.org
|
||||
[3]: https://opensource.com/article/19/7/introduction-gnu-autotools
|
||||
[4]: http://www.netbsd.org/developers/cvs-repos/cvs_intro.html#intro
|
||||
[5]: mailto:anoncvs@anoncvs.NetBSD.org
|
||||
[6]: http://pkgsrc.org/
|
||||
[7]: http://www.netbsd.org/docs/pkgsrc/getting.html
|
||||
[8]: http://pkgsrc.joyent.com/
|
||||
[9]: https://opensource.com/article/17/6/set-path-linux
|
||||
[10]: https://opensource.com/sites/default/files/uploads/pkgsrc-bootstrap.jpg (Bootstrapping pkgsrc on NetBSD)
|
||||
[11]: http://pkgin.net
|
Loading…
Reference in New Issue
Block a user