如何增强脚本以从多个CSV文件中查找

时间:2019-02-01 20:18:44

标签: bash command-line scripting text-processing python-textprocessing

我需要增强下面的脚本,该脚本需要一个包含几乎一百万行唯一代码的输入文件。在每一行中,它在3个查找文件中具有不同的值,我打算将它们作为逗号分隔的值添加到我的输出中。

以下脚本可以正常工作,但是需要花费数小时才能完成工作。我正在寻找一个真正的快速解决方案,该解决方案对系统也不会造成太大的负担。

#!/bin/bash
while read -r ONT
do
{

ONSTATUS=$(grep "$ONT," lookupfile1.csv | cut -d" " -f2)
CID=$(grep "$ONT." lookupfile3.csv | head -1 | cut -d, -f2)
line1=$(grep "$ONT.C2.P1," lookupfile2.csv | head -1 | cut -d"," -f2,7 | sed 's/ //')
line2=$(grep "$ONT.C2.P2," lookupfile2.csv | head -1 | cut -d"," -f2,7 | sed 's/ //')
echo "$ONT,$ONSTATUS,$CID,$line1,$line2" >> BUwithPO.csv
} & 
done < inputfile.csv

inputfile.csv包含以下行:

343OL5:LT1.PN1.ONT1
343OL5:LT1.PN1.ONT10
225OL0:LT1.PN1.ONT34
225OL0:LT1.PN1.ONT39
343OL5:LT1.PN1.ONT100
225OL0:LT1.PN1.ONT57

lookupfile1.csv包含:

343OL5:LT1.PN1.ONT100, Down,Locked,No
225OL0:LT1.PN1.ONT57, Up,Unlocked,Yes
343OL5:LT1.PN1.ONT1, Down,Unlocked,No
225OL0:LT1.PN1.ONT34, Up,Unlocked,Yes
225OL0:LT1.PN1.ONT39, Up,Unlocked,Yes

lookupfile2.csv包含:

225OL0:LT1.PN1.ONT34.C2.P1, +123125302766,REG,DigitMap,Unlocked,_media_BNT,FD_BSFU.xml,
225OL0:LT1.PN1.ONT57.C2.P1, +123125334019,REG,DigitMap,Unlocked,_media_BNT,FD_BSFU.xml,
225OL0:LT1.PN1.ONT57.C2.P2, +123125334819,REG,DigitMap,Unlocked,_media_BNT,FD_BSFU.xml,
343OL5:LT1.PN1.ONT100.C2.P11, +123128994019,REG,DigitMap,Unlocked,_media_ANT,FD_BSFU.xml,

lookupfile3.csv包含:

343OL5:LT1.PON1.ONT100.SERV1,12-654-0330
343OL5:LT1.PON1.ONT100.C1.P1,12-654-0330
343OL5:LT7.PON8.ONT75.SERV1,12-664-1186
225OL0:LT1.PN1.ONT34.C1.P1.FLOW1,12-530-2766
225OL0:LT1.PN1.ONT57.C1.P1.FLOW1,12-533-4019

输出为:

225OL0:LT1.PN1.ONT57, Up,Unlocked,Yes,12-533-4019,+123125334019,FD_BSFU.xml,+123125334819,FD_BSFU.xml
225OL0:LT1.PN1.ONT34, Up,Unlocked,Yes,12-530-2766,+123125302766,FD_BSFU.xml,
343OL5:LT1.PN1.ONT1, Down,Unlocked,No,,,
343OL5:LT1.PN1.ONT100, Down,Locked,No,,,
343OL5:LT1.PN1.ONT10,,,,
225OL0:LT1.PN1.ONT39, Up,Unlocked,Yes,,,

3 个答案:

答案 0 :(得分:4)

如您所见,瓶颈将在循环中多次执行grep。您可以通过创建具有关联数组的查找表来提高效率。
如果awk可用,请尝试以下操作:

[更新]

#!/bin/bash

awk '
FILENAME=="lookupfile1.csv" {
    sub(",$", "", $1);
    onstatus[$1] = $2
}
FILENAME=="lookupfile2.csv" {
    split($2, a, ",")
    if (sub("\\.C2\\.P1,$", "", $1)) line1[$1] = a[1]","a[6]
    else if (sub("\\.C2\\.P2,$", "", $1)) line2[$1] = a[1]","a[6]
}
FILENAME=="lookupfile3.csv" {
    split($0, a, ",")
    if (match(a[1], ".+\\.ONT[0-9]+")) {
        ont = substr(a[1], RSTART, RLENGTH)
        cid[ont] = a[2]
    }
}
FILENAME=="inputfile.csv" {
    print $0","onstatus[$0]","cid[$0]","line1[$0]","line2[$0]
}
' lookupfile1.csv lookupfile2.csv lookupfile3.csv inputfile.csv > BUwithPO.csv

{EDIT]

如果您需要指定文件的绝对路径,请尝试:

#!/bin/bash

awk '
FILENAME ~ /lookupfile1.csv$/ {
    sub(",$", "", $1);
    onstatus[$1] = $2
}
FILENAME ~ /lookupfile2.csv$/ {
    split($2, a, ",")
    if (sub("\\.C2\\.P1,$", "", $1)) line1[$1] = a[1]","a[6]
    else if (sub("\\.C2\\.P2,$", "", $1)) line2[$1] = a[1]","a[6]
}
FILENAME ~ /lookupfile3.csv$/ {
    split($0, a, ",")
    if (match(a[1], ".+\\.ONT[0-9]+")) {
        ont = substr(a[1], RSTART, RLENGTH)
        cid[ont] = a[2]
    }
}
FILENAME ~ /inputfile.csv$/ {
    print $0","onstatus[$0]","cid[$0]","line1[$0]","line2[$0]
}
' /path/to/lookupfile1.csv /path/to/lookupfile2.csv /path/to/lookupfile3.csv /path/to/inputfile.csv > /path/to/BUwithPO.csv

希望这会有所帮助。

答案 1 :(得分:4)

如果如注释中所述,由于缺少GNU gensub提供的awk而无法使用@tshiono提供的解决方案,则可以将gensub替换为两个调用带有临时变量的sub,以完成对所需后缀的修剪。

示例:

awk '
FILENAME=="lookupfile1.csv" {
    sub(",$", "", $1);
    onstatus[$1] = $2
}
FILENAME=="lookupfile2.csv" {
    split($2, a, ",")
    if (sub("\\.C2\\.P1,$", "", $1)) line1[$1] = a[1]","a[6]
    else if (sub("\\.C2\\.P2,$", "", $1)) line2[$1] = a[1]","a[6]
}
FILENAME=="lookupfile3.csv" {
    split($0, a, ",")
#     ont = gensub("(\\.ONT[0-9]+).*", "\\1", 1, a[1])
    sfx = a[1]
    sub(/^.*[.]ONT[^.]*/, "", sfx)
    sub(sfx, "", a[1])
#     cid[ont] = a[2]
    cid[a[1]] = a[2]
}
FILENAME=="inputfile.csv" {
    print $0","onstatus[$0]","cid[$0]","line1[$0]","line2[$0]
}
' lookupfile1.csv lookupfile2.csv lookupfile3.csv inputfile.csv > BUwithPO.csv

我已在与gensub相关的部分中注释了FILENAME=="lookupfile3.csv"的使用,并用gensub两次调用sub来代替sfx表达式(后缀)作为临时变量。

尝试一下,让我知道您是否可以使用它。

答案 2 :(得分:3)

Perl解决方案

下面的脚本是类似于awk解决方案,但用Perl写成的。 保存为filter.pl,并使其可执行。

#!/usr/bin/env perl

use strict;
use warnings;

my %lookup1;
my %lookup2_1;
my %lookup2_2;
my %lookup3;

while( <> ) {
    if ( $ARGV eq 'lookupfile1.csv' ) {
        # 225OL0:LT1.PN1.ONT34, Up,Unlocked,Yes
        # ^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^ 
        if (/^([^,]+),\s*(.*)$/) {
            $lookup1{$1} = $2;
        }
    } elsif ( $ARGV eq 'lookupfile2.csv' ) {
        # 225OL0:LT1.PN1.ONT34.C2.P1, +123125302766,REG,DigitMap,Unlocked,_media_BNT,FD_BSFU.xml,
        # ^^^^^^^^^^^^^^^^^^^^        ^^^^^^^^^^^^^                                  ^^^^^^^^^^^ 
        if (/^(.+ONT\d+)\.C2\.P1,\s*([^,]+),(?:[^,]+,){4}([^,]+)/) {
            $lookup2_1{$1} = "$2,$3";
        } elsif (/^(.+ONT\d+)\.C2\.P2,\s*([^,]+),(?:[^,]+,){4}([^,]+)/) {
            $lookup2_2{$1} = "$2,$3";
        }
    } elsif ( $ARGV eq 'lookupfile3.csv' ) {
        # 225OL0:LT1.PN1.ONT34.C1.P1.FLOW1,12-530-2766
        # ^^^^^^^^^^^^^^^^^^^^             ^^^^^^^^^^^
        if (/^(.+ONT\d+)[^,]+,\s*(.*)$/) {
            $lookup3{$1} = $2;
        }
    } else { # assume 'inputfile.csv'
        no warnings 'uninitialized'; # because not all keys ($_) have values in the lookup tables
        # 225OL0:LT1.PN1.ONT34
        chomp;
        print "$_,$lookup1{$_},$lookup3{$_},$lookup2_1{$_},$lookup2_2{$_}\n";        
    }
}

执行它像这样:

./filter.pl lookupfile{1,2,3}.csv inputfile.csv > BUwithPO.csv

重要的是,查找文件必须排在第一位(就像awk解决方案一样,因为) 他们构建了四个字典(在Perl中为哈希%lookup1%lookup2_1等。 然后从值inputfile.csv是针对这些字典匹配。