Search code examples
fileawkcut

separate file into several


I have a textfile that has several paragraphs, which I want to cut into separate files. It should work of a Linux system.

for example: first I have this File:

== 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

and the cutting it should have two files, named 15-02-2014.txt and 01-01-2014.txt containing only the textpart belonging to the date, one problem may be that there are linebreaks and empty lines in between, so a file could contain several text blocks

I thought there would be a solution using awk, but I don't know how to save it to different files. My other approach was using libreoffice writer, but it seems that its search and replace function does not allow inserting a pagebreak.


Solution

  • You can do like this:

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

    It will use the date after == as the name of the output file.