У меня есть два файла, и я пытаюсь присоединиться к ним в определенном месте. Я хотел бы присоединиться к ним в четвертом столбце второго файла, используя первый столбец первого. Это сводит меня с ума!
Вот что я пытаюсь:
join -j4 <(sort -k1 FirstFile.txt) <(sort -k4 SecondFile.txt)
FirstFile.txt:
24.136.152.171 US
24.136.152.171 US
24.136.152.171 US
SecondFile.txt
2014-08-03 00:00:00 User 24.136.152.171
2014-08-03 00:00:00 User 24.136.152.171
2014-08-03 00:00:00 User 24.136.152.171
Желаемый результат:
2014-08-03 00:00:00 User 24.136.152.171 US
2014-08-03 00:00:00 User 24.136.152.171 US
2014-08-03 00:00:00 User 24.136.152.171 US
Вы могли бы нам python.
Сохраните следующее в файле с именем join.py в своей домашней области:
ffile=open('FirstFile.txt','r').read().split('\n') # Open the first file, read it and split it into a list at the newline character
sfile=open('SecondFile.txt','r').read().split('\n') # Open the second file, read it and split it into a list at the newline character
minlen=min(len(ffile),len(sfile)) # Get the lengths of both, and return the minimum so it doesn't break if they are different lengths.
ofile = [] # Create an empty list.
for i in range (minlen): # Loop for the length of the shortest list.
ofile = ofile + [ffile[i]+sfile[i]] # Add the first item of the first list (the first line of the first file) to the first item of the second list (the first line of the second file).
outfile=open('outputfile','w') # Create an output file, called outputfile.txt in your home directory
outfile.write('\n'.join(ofile)) # Write to the output file.
, затем запустите его с помощью
python join.py