How to file split at a line number [closed]

扶醉桌前 提交于 2019-12-18 09:59:05

问题


I want to split a 400k line long log file from a particular line number.

For this question, lets make this an arbitrary number 300k.

Is there a linux command that allows me to do this (within the script)?

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!

On second thoughts this would be more suited to the superuser or serverfault site.


回答1:


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

will output xaa with 300k lines and xab with 100k lines, for an input with 400k lines.



来源:https://stackoverflow.com/questions/3066948/how-to-file-split-at-a-line-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!