比较2个文件,并在匹配后将file1中的值附加到file2的末尾

时间:2018-03-23 05:58:07

标签: python shell awk

我必须将FILE1的Column1与FILE2的值匹配,如果匹配,则将File1中的Column3追加到FILE2中每行的末尾。

我在下面列出了示例,我们非常感谢您的帮助。

FILE1

405869000009306,917010120346,3535080769880902
405869000013492,917010130771,8687780205056100
405869000014438,917010132781,8626280378318000

FILE2

9,=5|0|Pay,=,=,=1|1|918438953359,+918438953359@,=405869000009306,=20180304060000804+0530,=0,=0,=140,=0,=25,=0,=180304055956+22,=199,=1,=1,=-e-89-43-16-e367d-5a9b3e08-98b90,+918438953359@,=1|1|917019075052,=1|1|917010075000,=,=,=,=,=icid-value=14.137.67-1520123400.11094809;icid-generated-at;orig-ioi,



8,=1|1|918667657858,+918667657858@,=405869000013492,=1|1|919894455911,=,=,=20180304060000740+0530,=0,=1,=2,=0,=0,=0,=000000000000+00,=0,=1,=1,=1567756633_2338487864@2405:204:7a4e:2019:f339:d39b:1e12:915e,=sip:tn1ipsxm001.ims.mnc869.mcc405.3gppnetwork.org,=1|1|917010075000,=1|1|917010075009,mnc,=3GPP-E-UTRAN-FDD;405869006D039F831,=,=,=icid-value=11.274.6905-1520123400.57776869;icid-generated-at;orig-ioi,

输出

9,=5|0|Pay,=,=,=1|1|918438953359,+918438953359@,=405869000009306,=20180304060000804+0530,=0,=0,=140,=0,=25,=0,=180304055956+22,=199,=1,=1,=-e-89-43-16-e367d-5a9b3e08-98b90,+918438953359@,=1|1|917019075052,=1|1|917010075000,=,=,=,=,=icid-value=14.137.67-1520123400.11094809;icid-generated-at;orig-ioi,3535080769880902



8,=1|1|918667657858,+918667657858@,=405869000013492,=1|1|919894455911,=,=,=20180304060000740+0530,=0,=1,=2,=0,=0,=0,=000000000000+00,=0,=1,=1,=1567756633_2338487864@2405:204:7a4e:2019:f339:d39b:1e12:915e,=sip:tn1ipsxm001.ims.mnc869.mcc405.3gppnetwork.org,=1|1|917010075000,=1|1|917010075009,mnc,=3GPP-E-UTRAN-FDD;405869006D039F831,=,=,=icid-value=11.274.6905-1520123400.57776869;icid-generated-at;orig-ioi,8687780205056100

2 个答案:

答案 0 :(得分:0)

您可以尝试下面的shell脚本

#!/bin/bash
content1=`cat file1`
while read line2; do
        found=0
        for line1 in $content1; do
                first_column=`echo $line1|awk -F ',' '{print $1}'`
                if [[ $line2 = *"$first_column"* ]]; then
                        third_column=`echo $line1|awk -F ',' '{print $3}'`
                        echo $line2.$third_column
                        found=1
                        break
                fi
        done;
        if [[ $found == 0 ]]; then
                echo $line2;
        fi
done < file2

答案 1 :(得分:0)

您可以使用以下代码:

file1 = open("input1.txt","r")
file2 = open("input2.txt","r")

file2_array = [data for data in file2]

file = open("output.txt","a")
for row in file1:
    element_to_check = row.split(",")[0]
    for row_to_check in file2_array:
        if element_to_check in row_to_check:
            file.write('%s,%s'%(row_to_check, row.split(",")[2]) + '\n')

此代码从两个输入文件(我假设为文本文件)中读取,然后比较数据,如果条件满足,则将代码附加到行并将其写入输出文件。