如何在行号处拆分 [英] How to file split at a line number
问题描述
我想从特定的行号中分割出一个40万行的日志文件.
I want to split a 400k line long log file from a particular line number.
对于这个问题,让我们将其设为任意数字300k.
For this question, lets make this an arbitrary number 300k.
是否存在允许我执行此操作的Linux命令(在脚本内)?
Is there a linux command that allows me to do this (within the script)?
我知道split
让我按大小或行号将文件分成相等的部分,但这不是我想要的.我要在一个文件中的前300k,然后在第二个文件中的最后100k.
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.
任何帮助将不胜感激.谢谢!
Any help would be appreciated. Thanks!
再三考虑,这将更适合于超级用户或服务器故障站点.
推荐答案
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 -l 300000 file_name
对于具有40万行的输入,将输出30万行的xaa
和100k行的xab
.
will output xaa
with 300k lines and xab
with 100k lines, for an input with 400k lines.
这篇关于如何在行号处拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!