给定开始和结束行号,从文件中获取一系列行 [英] Get a range of lines from a file given the start and end line numbers
本文介绍了给定开始和结束行号,从文件中获取一系列行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从给定开始行号和结束行号的文件中提取一定数量的行.
I need to extract a set number of lines from a file given the start line number and end line number.
我怎样才能在 unix 下快速做到这一点(它实际上是 Solaris,所以 gnu 风格不可用).
How could I quickly do this under unix (it's actually Solaris so gnu flavour isn't available).
谢谢
推荐答案
打印第 6-10 行:
To print lines 6-10:
sed -n '6,10p' file
如果文件很大,并且结束行数比行数小,可以通过以下方式提高效率:
If the file is huge, and the end line number is small compared to the number of lines, you can make it more efficient by:
sed -n '10q;6,10p' file
从测试具有相当多行的文件:
From testing a file with a fairly large number of lines:
$ wc -l test.txt
368048 test.txt
$ du -k test.txt
24640 test.txt
$ time sed -n '10q;6,10p' test.txt >/dev/null
real 0m0.005s
user 0m0.001s
sys 0m0.003s
$ time sed -n '6,10p' test.txt >/dev/null
real 0m0.123s
user 0m0.092s
sys 0m0.030s
这篇关于给定开始和结束行号,从文件中获取一系列行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文