Не может выяснить команду JOIN

У меня есть два файла, и я пытаюсь присоединиться к ним в определенном месте. Я хотел бы присоединиться к ним в четвертом столбце второго файла с помощью первого столбца первого. Это сводит меня с ума!

Вот то, что я пробую:

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
0
задан 16 August 2014 в 06:00

2 ответа

Выходной формат по умолчанию join должен распечатать объединяющее поле, затем остающиеся поля от FILE1 и затем остающиеся поля от FILE2, если формат не определяется с -o. Далее, опция -j4 означает, что объединяющее поле является 4-м полем и в FILE1 и в FILE2. Таким образом, необходимо разделить -j4 к -1 1 -2 4.

Попытка это:

join -o '2.1 2.2 2.3 2.4 1.2' -2 4 -1 1 <(sort -k1 FirstFile.txt) <(sort -k4 SecondFile.txt)
2
ответ дан 5 August 2019 в 18:21

Вы могли нас 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
0
ответ дан 5 August 2019 в 18:21

Другие вопросы по тегам:

Похожие вопросы: