Some time ago on the internet to see an interview question, requirements are as follows:
The employee file records the work number and the name
Copy Code code as follows:
Cat Employee.txt:
Jason Smith
John Doe
Sanjay Gupta
Ashok Sharma
Record work numbers and wages in bonus documents
Copy Code code as follows:
Cat Bonus.txt:
100 $5,000
200 $
300 $3,000
400 $1,250
Two files are required to be merged and exported as follows:
Copy Code code as follows:
Ashok Sharma $1,250
Jason Smith $5,000
John Doe
Sanjay Gupta $3,000
This is required to be written in the shell, but my shell is not very good, using Python to achieve
Note, according to the title, in the output file also need to be sorted by initials, implementation code
Copy Code code as follows:
#! /usr/bin/env python
#coding =utf-8
FP01 = open ("Bonus.txt", "R")
A = []
For LINE01 in FP01:
A.append (LINE01)
FP02 = open ("Employee.txt", "R")
Fc02 = sorted (fp02, key = Lambda x:x.split () [1])
For LINE02 in FC02:
i = 0
While Line02.split () [0]!=a[i].split () [0]:
i + 1
Print '%s%s%s '% (Line02.split () [0], Line02.split () [1], Line02.split () [2], A[i].split () [1])
Fp01.close ()
Fp02.close ()