这是因为它会扫描到文件的末尾。打印后试试这个退出:
sed -ne '15690q;15689p' file
或者使用 awk:
awk 'NR>=15689 && NR<=15696{print} NR==15697{exit}' filename
只是为了好玩,我运行了@RichardHum 的时间,而我的时间在带有 SSD 驱动器的 OSX Mavericks 上完全相反:
#!/bin/bash -xv
seq 1 100000000 > file
time (head -50000000 file | tail -10) > /dev/null
time (sed -n '50000000q;49999991,50000000p' file) > /dev/null
time (awk 'NR>=49999991 && NR<=50000000{print} NR==50000001{exit}' file)
time (head -50000000 file | tail -10) > /dev/null
我得到了:
time (head -50000000 file | tail -10) > /dev/null
real 0m29.565s
user 0m35.711s
sys 0m0.733s
time (sed -n '50000000q;49999991,50000000p' file) > /dev/null
real 0m13.313s
user 0m13.162s
sys 0m0.150s
time (awk 'NR>=49999991 && NR<=50000000{print} NR==50000001{exit}' file)
real 0m7.433s
user 0m7.293s
sys 0m0.139s
time (head -50000000 file | tail -10) > /dev/null
real 0m29.560s
user 0m35.697s
sys 0m0.742s
我什至在最后运行了 head+tail 解决方案,以防第一次缓存没有好处,但它肯定慢了几英里!