[英]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.