Linux 如何在行号处拆分文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3066948/
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
How to file split at a line number
提问by denormalizer
I want to split a 400k line long log file from a particular line number.
我想从特定行号拆分 400k 行长的日志文件。
For this question, lets make this an arbitrary number 300k.
对于这个问题,让我们将其设为任意数字 300k。
Is there a linux command that allows me to do this (within the script)?
是否有允许我执行此操作的 linux 命令(在脚本中)?
I know split
lets me split the file in equal parts either by size or line numbers but that's not what I want. I want to the first 300k in one file and the last 100k in the second file.
我知道split
让我按大小或行号将文件分成相等的部分,但这不是我想要的。我想要一个文件中的前 300k 和第二个文件中的最后 100k。
Any help would be appreciated. Thanks!
任何帮助,将不胜感激。谢谢!
On second thoughts this would be more suited to the superuser or serverfault site.
再想一想,这更适合超级用户或 serverfault 站点。
采纳答案by academicRobot
file_name=test.log
# set first K lines:
K=1000
# line count (N):
N=$(wc -l < $file_name)
# length of the bottom file:
L=$(( $N - $K ))
# create the top of file:
head -n $K $file_name > top_$file_name
# create bottom of file:
tail -n $L $file_name > bottom_$file_name
Also, on second thought, split will work in your case, since the first split is larger than the second. Split puts the balance of the input into the last split, so
此外,再想一想,拆分将适用于您的情况,因为第一个拆分大于第二个。Split 把输入的余额放到最后一个 split 中,所以
split -l 300000 file_name
split -l 300000 file_name
will output xaa
with 300k lines and xab
with 100k lines, for an input with 400k lines.
将输出xaa
300k 行和xab
100k 行,输入 400k 行。