How do you split a filebase on a token?
Let's say you have a file containing texts (1 to N), separated by $. How to cut a file so that the end result is N files?
text1 with newlines $
text2 $, etc. $
textN
I am thinking something with awk or sed, but is there any unix application available that already does such a task?
Maybe a split -p
pattern?
Hmm. This may not be exactly what you want. It doesn't break the line, it only starts a new file when it sees the template. And it seems to be only supported on BSD related systems.
You can use something like:
awk 'BEGIN {RS = "$"} { ... }'
edit: You can find some inspiration for the part { ... }
here:
http://www.gnu.org/manual/gawk/html_node/Split-Program.html
edit: Thanks to the comment from dmckee, but csplit
also copies the entire line where the template occurs.
awk 'BEGIN {RS = "$"; ORS = "} {textNumber ++; print $ 0>" text "textNumber" .out "} 'fileName
Thanks to Bill Karwin for this idea.
Edit: add ORS = "" to avoid printing a new line at the end of each file.
If I read this correctly, the UNIX cut command can be used for this .
cut -d $ -f 1- filename
I might have the syntax slightly off, but that should tell cut that you are using $ separated fields and return fields 1 to the end.
You may need to avoid $.
awk -vRS="$" '{ print $0 > "text"t++".out" }' ORS="" file
using split command we can split using strings.
but the csplit command will allow you to split files based on regular expressions.