Если у вас есть память до sh всего файла ("max
" ниже):
$ awk '{
a[NR]=$0 # hash all the records
}
END { # after hashing
mid=int(NR/2) # compute the midpoint, int in case NR is uneven
for(i=1;i<=mid;i++) # iterate from start to midpoint
print a[i],a[mid+i] # output
}' file
Если у вас есть память до sh половины файла (" mid
"):
$ awk '
NR==FNR { # on 1st pass hash second half of records
if(FNR>1) { # we dont need the 1st record ever
a[FNR]=$0 # hash record
if(FNR%2) # if odd record
delete a[int(FNR/2)+1] # remove one from the past
}
next
}
FNR==1 { # on the start of 2nd pass
if(NR%2==0) # if record count is uneven
exit # exit as there is always even count of them
offset=int((NR-1)/2) # compute offset to the beginning of hash
}
FNR<=offset { # only process the 1st half of records
print $0,a[offset+FNR] # output one from file, one from hash
next
}
{ # once 1st half of 2nd pass is finished
exit # just exit
}' file file # notice filename twice
И, наконец, если у вас есть awk, скомпилированный в мозг червей (ie. Не так много памяти," min
"):
$ awk '
NR==FNR { # just get the NR of 1st pass
next
}
FNR==1 {
mid=(NR-1)/2 # get the midpoint
file=FILENAME # filename for getline
while(++i<=mid && (getline line < file)>0); # jump getline to mid
}
{
if((getline line < file)>0) # getline read from mid+FNR
print $0,line # output
}' file file # notice filename twice
Стандартный отказ от ответственности по getline
и контроль реальных ошибок не реализован.
Производительность:
I seq 1 100000000 > file
и тестирование работы вышеуказанных решений. Вывод был > /dev/null
, но запись в файл длилась около 2 с. max
производительность настолько низкая, что отпечаток памяти составляет 88% от моих 16 ГБ, поэтому он мог поменяться местами. Ну, я убил все браузеры и сбрил 7 секунд в реальном времени max
.
+------------------+-----------+-----------+
| which | | |
| min | mid | max |
+------------------+-----------+-----------+
| time | | |
| real 1m7.027s | 1m30.146s | 0m48.405s |
| user 1m6.387s | 1m27.314 | 0m43.801s |
| sys 0m0.641s | 0m2.820s | 0m4.505s |
+------------------+-----------+-----------+
| mem | | |
| 3 MB | 6.8 GB | 13.5 GB |
+------------------+-----------+-----------+
Обновление:
Я тестировал @DavidC.Rankin и @ EdMorton * 1034 Решения * и они работали, соответственно:
real 0m41.455s
user 0m39.086s
sys 0m2.369s
и
real 0m39.577s
user 0m37.037s
sys 0m2.541s
Печать Mem была примерно такой же, как у моего mid
. Кажется, стоит использовать wc
.