2 个脚本的输出中包含常用词的行

Lines with common words in outputs of 2 scripts

我需要 运行 两个 linux shell 脚本并从第二个脚本中获取包含与第一个输出中的行相同单词的行(不是整行是相同)。例如:

Script #1 output:
Router 1: Ip address 10.0.0.1
Router 2: Ip address 10.0.1.1
Router 3: Ip address 10.0.2.1

Script #2 output:
Router 1: Model: Cisco 2960
Router 2: Model: Juniper MX960
Router 5: Model: Huwei S3300

所以,最后我需要一个路由器列表,它们出现在两个输出中,但只有来自第二个脚本的行,即带有模型的行。

考虑到以上两个脚本输出分别是 stored/redirected 到 tmp1 和 tmp2。

下面的脚本将输出两个文件中存在的常见 Router X

#!/bin/bash
tmp1=""
tmp2=""
while read -r line
   do   
    routerName=$(echo "$line" | cut -d ":" -f 1)
    if grep -q "$routerName" "$tmp2"
       then
        # Instead of printing you can add any logic
        echo $routerName
    fi
done < "$tmp1"

将上面的脚本保存为filename.sh并传递参数

./filename.sh tmpScript1output_file tmpScript2output_file