打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何使用bash一个大文件分割成许多小文件?
  

如何使用bash一个大文件分割成许多小文件?



bash
我有一个文件,说all,有2000行,我希望它可以被分成4个小文件,行号1?500,501?1000,1001?1500 CodeGo.net,1501?2000。 或许,我可以做
cat all | head -500 >small1cat all | tail -1500 | head -500 >small2cat all | tail -1000 | head -500 >small3cat all | tail -500 >small4
但这种方式涉及到计算行数,这可能错误,当行数是不是一个好数字,或者当我们要分割的文件太多的小文件(例如:文件all与3241线,我们希望把它分割成7个文件,每个都有463线)。 有没有更好的办法做到这一点?
本文地址 :CodeGo.net/2693017/
-------------------------------------------------------------------------------------------------------------------------
1.如果要分割的文件,使用split
split -l 500 all all
将文件拆分成每个具有500线的几个文件。如果您想将文件分割成4个文件差不多大小的,用这样的:
split -l $(( $( wc -l < all ) / 4 + 1 )) all all

2.直视split命令,它应该做你想做的(及以上):
$ split --helpUsage: split [OPTION]... [INPUT [PREFIX]]Output fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; defaultsize is 1000 lines, and default PREFIX is 'x'. With no INPUT, or when INPUTis -, read standard input.Mandatory arguments to long options are mandatory for short options too. -a, --suffix-length=N generate suffixes of length N (default 2)  --additional-suffix=SUFFIX append an additional SUFFIX to file names. -b, --bytes=SIZE  put SIZE bytes per output file -C, --line-bytes=SIZE put at most SIZE bytes of lines per output file -d, --numeric-suffixes[=FROM] use numeric suffixes instead of alphabetic.         FROM changes the start value (default 0). -e, --elide-empty-files do not generate empty output files with '-n'  --filter=COMMAND write to shell COMMAND; file name is $FILE -l, --lines=NUMBER  put NUMBER lines per output file -n, --number=CHUNKS  generate CHUNKS output files. See below -u, --unbuffered  immediately copy input to output with '-n r/...'  --verbose   print a diagnostic just before each       output file is opened  --help  display this help and exit  --version output version information and exitSIZE is an integer and optional unit (example: 10M is 10*1024*1024). Unitsare K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).CHUNKS may be:N  split into N files based on size of inputK/N  output Kth of N to stdoutl/N  split into N files without splitting linesl/K/N output Kth of N to stdout without splitting linesr/N  like 'l' but use round robin distributionr/K/N likewise but only output Kth of N to stdout

3.像其他人有你split。所接受的命令替换是没有必要的。仅供参考,我加入了几乎什么一直请求。注意-n命令行来指定夹头,该数small*文件不包含正好500线split
$ seq 2000 > all$ split -n l/4 --numeric-suffixes=1 --suffix-length=1 all small$ wc -l small* 583 small1 528 small2 445 small3 444 small42000 total
另外,您也GNU并行:
$ < all parallel -N500 --pipe --cat cp {} small{#}$ wc -l small* 500 small1 500 small2 500 small3 500 small42000 total
正如你所看到的,这个咒语是GNU的并行实际上是most-的并行pipeline。恕我直言一款值得工具寻找到。
本文标题 :如何使用bash一个大文件分割成许多小文件?
本文地址 :CodeGo.net/2693017/  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Linux文件分割与合并:split & cat
Linux 下大文件切割与合并
快速高效!用Python批量分割PDF文件,让你的工作更轻松...
AE怎么导入SRT文件?不需要任何脚本插件,却能一键生成
perl 实现文件读取示例
Python3批量转换文本文件编码
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服