简体   繁体   中英

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.

I have included examples below and any help is appreciated.

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,

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

#!/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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM