In a python script:
A pragmatic solution
In case it is a "one time job", specific for one situation, the following works:
#!/usr/bin/env python3
with open(file1) as names:
names = sorted(names.readlines())
with open(file2) as data:
data = data.readlines()
for i in names:
item = i.replace("\n", "")+str([d[d.find(":"):].replace("\n", "") for d in data if d.startswith(i.split(":")[0])][0])
print(item)
Output:
Neeraj:149:[email protected]
Rahul:148:[email protected]
Tarun:143:[email protected]
Or, if you want to save the output directly into a file:
#!/usr/bin/env python3
with open(file1) as names:
names = sorted(names.readlines())
with open(file2) as data:
data = data.readlines()
with open(file3, "wt") as output:
for i in names:
output.write(i.replace("\n", "")+str([d[d.find(":"):].replace("\n", "") for d in data if d.startswith(i.split(":")[0])][0])+"\n")
As you probably know, copy the script into an empty file, set the path to the file 1-2 (3) (between quotes), save it as combine.py
, run it by the command:
python3 /path/to/combine.py
A more database-worthy solution
Looking at the two files, we are actually dealing with databases, with the first field as a key. The following script is more flexible and covers a more flexible way of making reports of the two files, in situations (for example) where we would have more fields than here is the case.
If we would add an extra ("characterizing") field to the second file:
Neeraj:[email protected]:Loves to Cook
Rahul:[email protected]:Collects empty bottles
Tarun:[email protected]:Weares his glasses upside down
We might want to add the characterization instead of the email address, or both. That would ask for a script like:
#!/usr/bin/env python3
db1 = "/path/to/file1"; db2 = "/path/to/file2"
with open(db1) as data1:
rc = [l.replace("\n", "").split(":") for l in data1.readlines()]
with open(db2) as data2:
records2 = [l.replace("\n", "").split(":") for l in data2.readlines()]
uniques = sorted(set(item[0] for item in rc)) # find keys
report = []
for i in uniques:
database_1 = [r for r in rc if r[0] == i][0]
database_2 = [r for r in records2 if r[0] == i][0]
# -----------------------------------------------------------------------
# set the required fields for report here:
new_record = i, database_1[1], database_2[1]
# -----------------------------------------------------------------------
report.append((":").join(new_record))
for item in report:
print(item)
Result
If we set:
new_record = i, database_1[1], database_2[2]
The result is:
Neeraj:149:Loves to Cook
Rahul:148:Collects empty bottles
Tarun:143:Weares his glasses upside down
But if we set:
new_record = i, database_1[1], database_2[1]
The result is:
Neeraj:149:[email protected]
Rahul:148:[email protected]
Tarun:143:[email protected]
And if we set:
new_record = i, database_1[1], database_2[1], database_2[2]
The result is :
Neeraj:149:[email protected]:Loves to Cook
Rahul:148:[email protected]:Collects empty bottles
Tarun:143:[email protected]:Weares his glasses upside down