如何从unix/linux中的大文件中提取第一个x-megabyte?

ump*_*mps 6 unix linux command-line dd

我有一个大文件,我只对前几兆字节感兴趣.

如何从unix/linux中的大文件中提取第一个x-megabyte并将其放入单独的文件中?

(我知道split命令可以将文件拆分成多个部分.使用bash脚本我可以删除我不想要的部分.我希望更简单的方法)

seh*_*ehe 13

例如

 dd if=largefile count=6 bs=1M > largefile.6megsonly
Run Code Online (Sandbox Code Playgroud)

1M拼写假设GNU DD.否则,你可以做到

 dd if=largefile count=$((6*1024)) bs=1024 > largefile.6megsonly
Run Code Online (Sandbox Code Playgroud)

这再次假设bash式算术 评估.


tec*_*oke 9

Head使用二进制文件,语法比dd更整洁.

head -c 2M input.file > output.file
Run Code Online (Sandbox Code Playgroud)

如果你想要文件的结尾,Tail的工作方式相同.


小智 5

在 Mac (Catalina) 上,headandtail命令似乎不采用大写或小写的 m (mega) 和 g (giga) 等修饰符,但会采用大整数字节计数,例如 50 MB

head -c50000000 inputfile.txt > outputfile.txt
Run Code Online (Sandbox Code Playgroud)