linux中根据内容拆分文件 [英] Splitting a file in linux based on content

查看:15
本文介绍了linux中根据内容拆分文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约 400 mb 的电子邮件转储.我想将其拆分为 .txt 文件,每个文件中包含一封邮件.每封电子邮件都以指定文档类型的标准 HTML 标题开头.

I have an email dump of around 400mb. I want to split this into .txt files, consisting of one mail in each file. Every e-mail starts with the standard HTML header specifying the doctype.

这意味着我将不得不根据上述标题拆分我的文件.我如何在 linux 中处理它?<​​/p>

This means I will have to split my files based on the above said header. How do I go about it in linux?

推荐答案

如果您有 mail.txt

$ cat mail.txt
<html>
    mail A
</html>

<html>
    mail B
</html>

<html>
    mail C
</html>

运行 csplit 以按

$ csplit mail.txt '/^<html>$/' '{*}'

 - mail.txt    => input file
 - /^<html>$/  => pattern match every `<html>` line
 - {*}         => repeat the previous pattern as many times as possible

检查输出

$ ls
mail.txt  xx00  xx01  xx02  xx03

<小时>

如果你想用 awk

$ awk '/<html>/{filename=NR".txt"}; {print >filename}' mail.txt
$ ls
1.txt  5.txt  9.txt  mail.txt

这篇关于linux中根据内容拆分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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