如何在 bash shell 中从文件的两个不同行中提取两个不同的关键字?

mht*_*que 2 bash awk sed

我有一个名为 data.txt 的文件。当我阅读该文件时,内容如下所示。

$ cat data.txt 
name: linuxVol
id: 6
type: Linux
dir excludes: .snapshot*
              ~snapshot*
              .zfs
              .isilon
              .ifsvar
              .lustre
inode: 915720
free_space: 35.6TiB (auto)
total_capacity: 95.0TiB (auto)
number_of_files: 5,789,643
number_of_dirs: 520,710
mounts: https://server1.example.com:30002: /mnt/tube
Run Code Online (Sandbox Code Playgroud)

如何Linux从第三行和server1.example.com最后一行提取关键字,然后将它们表示在同一行中并用空格分隔?输出应显示如下所示

Linux server1.example.com
Run Code Online (Sandbox Code Playgroud)

我尝试做这样的事情但不知道如何提取server1.example.com

cat data.txt | egrep "type|mounts" | awk '{print $NF}' | tr "\n" " "
Run Code Online (Sandbox Code Playgroud)

输出是:

Linux /mnt/tube
Run Code Online (Sandbox Code Playgroud)

我的预期输出:

Linux server1.example.com
Run Code Online (Sandbox Code Playgroud)

用 AWK/SED 解决它对我有用。

谢谢你!

The*_*ird 5

使用gnu-awk您还可以将字段分隔符设置为:后跟 1 个或多个空格。

然后检查第一个字段的类型或安装,对于安装,使用捕获组来获取 https:// 部分之后的部分

鉴于行的顺序相同,并且两个关键字都存在,您可以连接这些值。

awk -F ":[[:space:]]+" '
$1 == "type" {s = $2}
$1 == "mounts" && match($2, /https?:\/\/([^[:space:]:]+)/, a) {s = s " " a[1]}
END {print s}
' data.txt
Run Code Online (Sandbox Code Playgroud)

输出

Linux server1.example.com
Run Code Online (Sandbox Code Playgroud)