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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 20:08:40  来源:igfitidea点击:

How to file split at a line number

linuxshellsplitfilesplitting

提问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 splitlets 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 xaawith 300k lines and xabwith 100k lines, for an input with 400k lines.

将输出xaa300k 行和xab100k 行,输入 400k 行。