Reputation: 3394
Let's say you got a file containing texts (from 1 to N) separated by a $ How can a slit the file so the end result is N files?
text1 with newlines $
text2 $etc... $
textN
I'm thinking something with awk or sed but is there any available unix app that already perform that kind of task?
Upvotes: 2
Views: 4470
Reputation: 1099
using split command we can split using strings.
but csplit command will allow you to slit files basing on regular expressions as well.
Upvotes: 1
Reputation: 3394
awk 'BEGIN{RS="$"; ORS=""} { textNumber++; print $0 > "text"textNumber".out" }' fileName
Thank to Bill Karwin for the idea.
Edit : Add the ORS="" to avoid printing a newline at the end of each files.
Upvotes: 3
Reputation: 562631
Maybe split -p
pattern?
Hmm. That may not be exactly what you want. It doesn't split a line, it only starts a new file when it sees the pattern. And it seems to be supported only on BSD-related systems.
You could use something like:
awk 'BEGIN {RS = "$"} { ... }'
edit: You might find some inspiration for the { ... }
part here:
http://www.gnu.org/manual/gawk/html_node/Split-Program.html
edit: Thanks to comment from dmckee, but csplit
also seems to copy the whole line on which the pattern occurs.
Upvotes: 2
Reputation: 88806
If I'm reading this right, 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're using $ separated fields and to return fields 1 through the end.
You may need to escape the $.
Upvotes: 1