简体   繁体   English

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

[英]compare 2 files and append a value from file1 to end of file2 after match

I have to match Column1 of FILE1 with values of FILE2 and if it matches, append Column3 from FILE1 to end of each line in FILE2. 我必须将FILE1的Column1与FILE2的值相匹配,如果匹配,则将FILE3的Column3附加到FILE2中每一行的末尾。

I have included examples below and any help is appreciated. 我在下面提供了示例,希望能对您有所帮助。

FILE1 文件1

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

FILE2 文件2

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,

OUTPUT 输出值

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

You can try the shell script below 您可以尝试下面的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

Hi use the below code: 嗨,使用以下代码:

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')

This code is reading from the two input files(i am assuming as text files) and then comparing the data and if the condition is satisfied, then it appends the code to the line and writes it to a output file. 这段代码是从两个输入文件(我假设是文本文件)中读取的,然后比较数据,如果满足条件,则将代码追加到该行,并将其写入输出文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较2个文件并删除file2中与file1中找到的值匹配的任何行 - Compare 2 files and remove any lines in file2 when they match values found in file1 将N行从File1复制到File2,然后删除File1中的复制行 - Copy N lines from File1 to File2, then delete copied lines in File1 获取文件1的相对路径(相对于文件2的路径,文件1在文件2的子文件夹中) - Get relative path of file1 (relative to path of file2, file1 is in subfolder of file2) 使用 filecmp.cmp(file1, file2) 将文件与文件列表进行比较 - Comparing file with a list of files using filecmp.cmp(file1, file2) 我有 2 个文件。 File1 由汽车名称组成,file2 由汽车 model 组成。如何让 file2 与 file1 具有相同的排列? - I have 2 files. File1 consist of car name and file2 consist of the car model. How do i get file2 to have the same arrangement as file1? 在Windows下调用shutil.copystat(file1,file2)后,文件修改时间不相等 - File modification times not equal after calling shutil.copystat(file1, file2) under Windows 使用循环在 file2 中从 file1 中搜索名称并写入 file3 - Using loops to search for names from file1 in file2 and writing to file3 如何用python中的file2中的行替换file1中的指定行 - How to replace specified lines from file1 with lines from file2 in python 在tkinter中从file1按下按钮时如何跳转运行/执行file2 - How jump run/execute file2 when a button is pressed from file1 in tkinter 从文件1 x文件2生成每个单词的笛卡尔积 - Generate Cartesian product of each word from file1 x file2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM