compare 2 files and append a value from file1 to end of file2 after match
我必须将 FILE1 的 Column1 与 FILE2 的值匹配,如果匹配,请将 FILE1 中的 Column3 附加到 FILE2 中每一行的末尾。
我在下面提供了示例,感谢您提供任何帮助。
文件1
1 2 3 | 405869000009306,917010120346,3535080769880902 405869000013492,917010130771,8687780205056100 405869000014438,917010132781,8626280378318000 |
文件2
1 2 3 4 5 | 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, |
输出
1 2 3 4 5 | 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 |
您好,请使用以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 | 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]) + '\ ') |
此代码从两个输入文件中读取(我假设为文本文件),然后比较数据,如果满足条件,则将代码附加到该行并将其写入输出文件。 你可以试试下面的shell脚本
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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