Linux 在文件中间显示特定行的快速 unix 命令?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/191364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Quick unix command to display specific lines in the middle of a file?
提问by matt b
Trying to debug an issue with a server and my only log file is a 20GB log file (with no timestamps even! Why do people use System.out.println()
as logging? In production?!)
尝试调试服务器的问题,我唯一的日志文件是一个 20GB 的日志文件(甚至没有时间戳!为什么人们使用System.out.println()
日志记录?在生产中?!)
Using grep, I've found an area of the file that I'd like to take a look at, line 347340107.
使用 grep,我找到了我想查看的文件区域,第 347340107 行。
Other than doing something like
除了做类似的事情
head -<$LINENUM + 10> filename | tail -20
... which would require head
to read through the first 347 million lines of the log file, is there a quick and easy command that would dump lines 347340100 - 347340200 (for example) to the console?
...这需要head
通读日志文件的前 3.47 亿行,是否有一个快速简便的命令可以将 347340100 - 347340200(例如)行转储到控制台?
updateI totally forgot that grep can print the context around a match ... this works well. Thanks!
更新我完全忘记了 grep 可以打印匹配的上下文……这很好用。谢谢!
采纳答案by matt b
with GNU-grep you could just say
使用 GNU-grep 你可以说
grep --context=10 ...
回答by itsmatt
What about:
关于什么:
tail -n +347340107 filename | head -n 100
I didn't test it, but I think that would work.
我没有测试它,但我认为这会起作用。
回答by mweerden
With sed -e '1,N d; M q'
you'll print lines N+1 through M. This is probably a bit better then grep -C
as it doesn't try to match lines to a pattern.
随着sed -e '1,N d; M q'
你打印线N + 1到M.这可能是一个好一点,然后grep -C
,因为它不尝试匹配行的模式。
回答by Sklivvz
回答by Luka Marinko
I'd first split the file into few smaller ones like this
我首先将文件分成几个像这样的小文件
$ split --lines=50000 /path/to/large/file /path/to/output/file/prefix
and then grep on the resulting files.
然后对生成的文件进行 grep。
回答by unwind
No there isn't, files are not line-addressable.
不,没有,文件不可行寻址。
There is no constant-time way to find the start of line nin a text file. You must stream through the file and count newlines.
没有固定时间的方法可以在文本文件中找到第n行的开头。您必须流式传输文件并计算换行符。
Use the simplest/fastest tool you have to do the job. To me, using head
makes muchmore sense than grep
, since the latter is way more complicated. I'm not saying "grep
is slow", it really isn't, but I would be surprised if it's faster than head
for this case. That'd be a bug in head
, basically.
使用最简单/最快的工具来完成这项工作。对我来说,使用head
使多不是更有意义grep
,因为后者的方式更加复杂。我不是说“grep
很慢”,确实不是,但如果它比head
这种情况快,我会感到惊讶。那将是一个错误head
,基本上。
回答by WCC
# print line number 52
sed -n '52p' # method 1
sed '52!d' # method 2
sed '52q;d' # method 3, efficient on large files
method 3 efficient on large files
方法3对大文件有效
fastest way to display specific lines
显示特定行的最快方法
回答by pixelbeat
sed will need to read the data too to count the lines. The only way a shortcut would be possible would there to be context/order in the file to operate on. For example if there were log lines prepended with a fixed width time/date etc. you could use the lookunix utility to binary search through the files for particular dates/times
sed 也需要读取数据来计算行数。唯一可能的快捷方式是在文件中存在上下文/顺序以进行操作。例如,如果日志行前面带有固定宽度的时间/日期等,您可以使用lookunix 实用程序对特定日期/时间的文件进行二进制搜索
回答by sehe
I prefer just going into less
and
我更喜欢进入less
和
- typing 50%to goto halfway the file,
- 43210Gto go to line 43210
:43210
to do the same
- 键入50%转到文件的一半,
- 43210G转到第 43210 行
:43210
做同样的事情
and stuff like that.
和类似的东西。
Even better: hit vto start editing (in vim, of course!), at that location. Now, note that vim
has the same key bindings!
更好的是:点击v开始编辑(当然是在 vim 中!),在那个位置。现在,请注意vim
具有相同的键绑定!
回答by Keithel
Building on Sklivvz' answer, here's a nice function one can put in a .bash_aliases
file. It is efficient on huge files when printing stuff from the front of the file.
以 Sklivvz 的回答为基础,这是一个可以放入.bash_aliases
文件的不错的函数。从文件前面打印内容时,它对大文件很有效。
function middle()
{
startidx=
len=
endidx=$(($startidx+$len))
filename=
awk "FNR>=${startidx} && FNR<=${endidx} { print NR\" \"$0 }; FNR>${endidx} { print \"END HERE\"; exit }" $filename
}