Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you split a file base on a token?

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?

like image 817
Julien Grenier Avatar asked Oct 20 '25 04:10

Julien Grenier


1 Answers

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.

like image 126
Julien Grenier Avatar answered Oct 22 '25 17:10

Julien Grenier