我有以下文件:

$ cat vehicles.lst


server.name.johnny

ford.mustang: 849448 found
gm.dennalie: 8548 found
Tesla.Y: 34343 found
################################
server.name.alex

ford.mustang: 849 found
gm.dennalie: 855698 found
Tesla.Y: 343 found
#######################

我正在处理这份文件.我想打印特定数字以上的车辆数量,并获得包含这些数字的服务器的名称.最好使用一行命令来解决这个问题.下面的内容将给我提供使用if条件的车辆数量,但我如何打印与该输出相关联的服务器名称.请帮帮忙

cat vehicles.lst | awk '{ if($2 > 80000) print $0}'  
ford.mustang: 849448 found
gm.dennalie: 855698 found

我期待这样的输出:

$cat vehicles.lst | awk '{ if($2 > 80000) print $0}' (need extra solution here to print the server name)

server.name.johnny
ford.mustang: 849448 found

server.name.alex
gm.dennalie: 855698 found

推荐答案

cat vehicles.lst | awk '{ if($2 > 80000) print $0}'

GNU AWK可以自己读取文件,所以你不用cat在这里,你可以做

awk '{ if($2 > 80000) print $0}' vehicles.lst

更简明的过滤方式是只说明条件,即

awk '$2 > 80000' vehicles.lst

其中,对于满足条件的行,将执行print的默认操作.

为了知道给定记录属于哪个服务器,输出服务器名称就足够了,这可以通过创建逻辑OR条件来实现

awk '/^server/ || $2 > 80000' vehicles.lst

其中vehicles.lst个内容是

server.name.johnny

ford.mustang: 849448 found
gm.dennalie: 8548 found
Tesla.Y: 34343 found
################################
server.name.alex

ford.mustang: 849 found
gm.dennalie: 855698 found
Tesla.Y: 343 found

给出输出

server.name.johnny
ford.mustang: 849448 found
server.name.alex
gm.dennalie: 855698 found

说明:如果以server开头或第二个字段值高于80000,则输出行.注意,这将输出all台服务器,即使这些服务器没有任何记录,其数量超过80000.

(tested in GNU Awk 5.1.0)

Linux相关问答推荐

C++17/Linux:信号未解锁单独线程中被阻止的网络套接字调用

C++17:G++8.5版似乎无法正确生成无符号64位伪随机整数

如何更正我的CMakeLists.txt,使我的项目同时构建在Linux和Windows上?

如何使用 sed 和 awk 命令提取模式?

未找到 SDL_Vertex 和 SDL_RenderGeometry

Google Cloud Ops Agent Mongo 集成错误 - AuthenticationFailed:SCRAM 身份验证失败,storedKey 不匹配

$RANDOM 变量在输出通过管道后似乎没有改变

如何在Linux中将文件的特定行号中的数字乘以2(双)?

我有 ext2 格式的文件系统图像.我喜欢在终端中读取 Linux 特别是 Ubuntu 中的所有文件系统 struct 数据.有没有工具>

MessageBox 的 GTK 实现

为什么 Linux (x86) 的页面大小是 4 KB,这是如何计算的?

使用 shell 脚本获取 MAC 地址

为什么 Linux 不通过 TSS 使用硬件上下文切换?

使用 Scp 时防止覆盖文件

低功耗蓝牙:在 linux 中监听通知/指示

使用 cmake 构建错误:找不到 -lpthreads

如何从 Linux 中的用户空间访问物理地址?

Windows 开发环境值得付出代价吗?

在Linux中使用空格设置环境变量

Linux 上的 NuGet:获取响应流时出错