匹配两个文件中的字符串,并在第一个文件中向第二个文件的行尾添加匹配字符串

时间:2018-07-27 11:49:48

标签: linux bash awk grep

所以这有点棘手,我花了一点时间试图弄清楚。

我有两个不同的文件,其中一个是rackdiag格式的文件,

#file1
rackdiag {
  rack {
    42U;
    description = "1.1.1";
    1: "serverone" [4U];
    5: servertwo [2U];
    7: serverthree\nblah [3U];
  }
  rack {
    42U;
    description = "1.1.2";
    1: servertwoone [4U];
    5: "servertwotwo" [2U];
   }
 }

等等。

另一个是这样的服务器名称列表:

#file2
serverone.domain.com
servertwo.domain.com
serverthree.domain.com

我正在尝试在两个文件之间匹配字符串,并将带有匹配字符串的行从第一个文件插入到第二个文件的末尾,并添加了一些附加内容。我希望它像这样在第二个文件中结束:

serverone.domain.com #1.1.1 1: "serverone" [4U];
servertwo.domain.com #1.1.1 5: servertwo [2U];
servertwoone.domain.com #1.1.2 1: servertwoone [4U]; 

我设法做到了这一点:

#!/bin/bash

cat serverlist.txt | while read line;
do
#grep for matching strings and output entire line when match found to $line2 variable
line2=$(grep -w "$line" row01.txt)
echo "$line "#" $line2" 
done > halp.txt
exit

哪个输出:

servertwo.domain.com #5: servertwo.domain.com [2U];

但是我注意到由于某些原因,它缺少一些应该匹配的东西。

就像,在实际文件中,我有这一行

   33: servername [2U];

第二行中的这一行:

servername.blahhosting.com

当我尝试运行脚本时,输出仅为:

servername.blahhosting.com #

有人能帮助我让1.1.1 / 1.1.2等出现在输出中并弄清楚为什么它可能缺少某些匹配的行吗?

非常感谢您!

编辑1:

rackdiag {
   rack {
       42U;
       description = "5.1.1";
       1: servertwoone [4U];
       1: "servertwoone" [4U];
       1: servertwoone\nserveronetwo [4U];
       1: "servertwoone\nserveronetwo" [4U];
       1: servertwo-1\nserverone1 [4U];
       1: "servertwo-2\nserverone2" [4U];
       1: servertwoone-1 [4U];
       1: servertwoone-2 [4U];
       1: servertwoone1 [4U];
       1: servertwoone2 [4U];
       1: servertwoone;
   }
   rack {
       42U;
       description = "5.1.2";
       1: server two one [4U];
       1: servertwoone [4U];
       1: server.two.one [4U];
   }
}

如果没有[2U]等,并且末尾为空白,则表示为[1U]

对于带有\ n的名称,这意味着服务器在物理外壳上具有多个标签。 我认为就是这样

1 个答案:

答案 0 :(得分:0)

您的问题尚不清楚,但这是正确的方法,也是解决您的问题的起点:

$ cat tst.awk
NR==FNR {
    if ( $1 == "description" ) {
        desc = $NF
        gsub(/^"|";$/,"",desc)
    }
    else if ( $1 ~ /^[0-9]+:/ ) {
        nmbr = $1
        sub(/^[[:space:]]*[^[:space:]]+[[:space:]]+/,"")

        if ( $NF ~ /\[.*\];$/ ) {
            blob = $NF
            sub(/[^[:space:]]+$/,"")
        }
        else {
            blob = "[1U];"
        }
        sub(/[[:space:]]+$/,"")

        numSrvrs = split($0,srvrs,/\\n/)
        for (srvrNr=1; srvrNr<=numSrvrs; srvrNr++) {
            srvr = srvrs[srvrNr]
            gsub(/^"|"$/,"",srvr)
            srvr2data[srvr] = "#" desc " " nmbr " " $0 " " blob
            printf "TRACE: srvr2data[%s] = <%s>\n", srvr, srvr2data[srvr]
        }
    }
    next
}
{
    srvr = $0
    sub(/\..*/,"",srvr)
    print $0, srvr2data[srvr]
}

针对前两个示例输入文件运行时:

$ awk -f tst.awk file1 file2
TRACE: srvr2data[serverone] = <#1.1.1 1: "serverone" [4U];>
TRACE: srvr2data[servertwo] = <#1.1.1 5: servertwo [2U];>
TRACE: srvr2data[serverthree] = <#1.1.1 7: serverthree\nblah [3U];>
TRACE: srvr2data[blah] = <#1.1.1 7: serverthree\nblah [3U];>
TRACE: srvr2data[servertwoone] = <#1.1.2 1: servertwoone [4U];>
TRACE: srvr2data[servertwotwo] = <#1.1.2 5: "servertwotwo" [2U];>
serverone.domain.com #1.1.1 1: "serverone" [4U];
servertwo.domain.com #1.1.1 5: servertwo [2U];
serverthree.domain.com #1.1.1 7: serverthree\nblah [3U];

使用第三个输入文件(问题中的Edit 1)运行时,没有关联的“ file2”(因为您没有提供文件),因此您得到的只是将跟踪输出作为第一个文件中的数据正在填充:

$ awk -f tst.awk file3 /dev/null
TRACE: srvr2data[servertwoone] = <#5.1.1 1: servertwoone [4U];>
TRACE: srvr2data[servertwoone] = <#5.1.1 1: "servertwoone" [4U];>
TRACE: srvr2data[servertwoone] = <#5.1.1 1: servertwoone\nserveronetwo [4U];>
TRACE: srvr2data[serveronetwo] = <#5.1.1 1: servertwoone\nserveronetwo [4U];>
TRACE: srvr2data[servertwoone] = <#5.1.1 1: "servertwoone\nserveronetwo" [4U];>
TRACE: srvr2data[serveronetwo] = <#5.1.1 1: "servertwoone\nserveronetwo" [4U];>
TRACE: srvr2data[servertwo-1] = <#5.1.1 1: servertwo-1\nserverone1 [4U];>
TRACE: srvr2data[serverone1] = <#5.1.1 1: servertwo-1\nserverone1 [4U];>
TRACE: srvr2data[servertwo-2] = <#5.1.1 1: "servertwo-2\nserverone2" [4U];>
TRACE: srvr2data[serverone2] = <#5.1.1 1: "servertwo-2\nserverone2" [4U];>
TRACE: srvr2data[servertwoone-1] = <#5.1.1 1: servertwoone-1 [4U];>
TRACE: srvr2data[servertwoone-2] = <#5.1.1 1: servertwoone-2 [4U];>
TRACE: srvr2data[servertwoone1] = <#5.1.1 1: servertwoone1 [4U];>
TRACE: srvr2data[servertwoone2] = <#5.1.1 1: servertwoone2 [4U];>
TRACE: srvr2data[servertwoone;] = <#5.1.1 1: servertwoone; [1U];>
TRACE: srvr2data[server two one] = <#5.1.2 1: server two one [4U];>
TRACE: srvr2data[servertwoone] = <#5.1.2 1: servertwoone [4U];>
TRACE: srvr2data[server.two.one] = <#5.1.2 1: server.two.one [4U];>

您没有告诉我们“ [4U]”字段是什么,所以我将其命名为blob-显然将其更改为任何内容。