linux - 在标题后打印文件中的第二个字段。

标签 linux bash shell awk

我有一个文件具有以下结构的场景:

This is the list of all the employees and their desk numbers
Please dont exchange the chairs or drawers
Handle the furniture around with care
Name    Desk No
----    -------
Joe     445
May     560
Sue     669

我只需要获取办公 table 号码。我正在考虑使用 ---- 作为过滤器并在后面的文本之后打印第二列。有更好的解决方案吗?

最佳答案

另一种方法:

$ awk 'p{print $2}$1=="----"{p=1}' file
445
560
669

这使用变量 p 作为打印标志(默认值为零)。只有当我们将 p 设置为非零值时, block print $2 才会执行。我们在看到第一个字段为 ----- 的行后设置 p

关于linux - 在标题后打印文件中的第二个字段。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15271218/

相关文章:

linux - Shell 脚本打印特定索引的行值

bash - 多次通过 stdin 将参数传递给 shell 脚本

linux - 将 STDERR 重定向到一个变量

从/dev/urandom 读取的 Bash 脚本

linux - 询问收件人、主题和正文的 Bash 邮件脚本

regex - 仅使用 'sed' 而不使用 'awk' 从记录中选择特定列

c - 共享内存

linux - 拒绝访问除几个主机之外的所有 Internet 主机

java - sdkman gradle 3.4无法确定java版本

bash - 在 unix/linux shell 中进行模式匹配时如何使用反向或负通配符?