将文件分成几个

时间:2014-07-21 08:58:29

标签: file awk cut

我有一个包含多个段落的文本文件,我想将其剪切成单独的文件。它应该适用于Linux系统。

例如: 首先我有这个文件:

== 01.01.2014 ==
this is the text that should 
go into

file one
and i like file one to be namend 01-01-2014

== 15.02.2014 ==
and this is the text that should
end up 
in file two

where file two is named 
15-02-2014

并且剪切它应该有两个文件,名为15-02-2014.txt和01-01-2014.txt只包含属于日期的textpart,一个问题可能是在它们之间有换行符和空行,因此文件可以包含多个文本块

我以为会有一个使用awk的解决方案,但我不知道如何将它保存到不同的文件中。 我的另一种方法是使用libreoffice编写器,但似乎它的搜索和替换功能不允许插入分页符。

1 个答案:

答案 0 :(得分:4)

你可以这样做:

awk '/^==/ {out=$2} {print > out".txt"}' file

它将使用==之后的日期作为输出文件的名称。