如何将文件拆分为n个部分 [英] How do I split a file into n no of parts

查看:63
本文介绍了如何将文件拆分为n个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个连续几行的文件.我想将文件拆分为n个具有特定名称的文件.每个文件中存在多少行都没有关系.我只想要特定数量的文件(例如5).这里的问题是原始文件中的行数一直在变化.因此,我无需计算行数,只需将文件分成5部分即可.如果可能的话,我们必须将它们各自发送到不同的目录中.

I have a file contining some no of lines. I want split file into n no.of files with particular names. It doesn't matter how many line present in each file. I just want particular no.of files (say 5). here the problem is the no of lines in the original file keep on changing. So I need to calculate no of lines then just split the files into 5 parts. If possible we have to send each of them into different directories.

推荐答案

在bash中,您可以使用split命令根据所需的行数对其进行拆分.您可以使用wc命令找出所需的行数.这是wcsplit组合在一起的一行.

In bash, you can use the split command to split it based on number of lines desired. You can use wc command to figure out how many lines are desired. Here's wc combined with with split into one line.

例如,将onepiece.log分为5个部分

    split -l$((`wc -l < onepiece.log`/5)) onepiece.log onepiece.split.log -da 4

这将创建类似onepiece.split.log0000 ...

This will create files like onepiece.split.log0000 ...

注意:bash划分为四舍五入,因此,如果还有余数,将有第6个部分文件.

Note: bash division rounds down, so if there is a remainder there will be a 6th part file.

这篇关于如何将文件拆分为n个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆