effbiae
effbiae

Reputation: 1157

What csplit-like command lets me create several files from one text file?

Are there any tools to do what I want already? I'd like to split the file 'one' at lines starting with @, followed by a filename:

$ cat one
@header
-- header --
@body
-- body --
@body isn't a split point
@footer
-- footer --

$ splitit one
$ ls
body footer header one 
$ cat header
-- header --
$ cat body
-- body --
@body isn't a split point
$ cat footer
-- footer --

What is splitit?

Use case:

Upvotes: -1

Views: 448

Answers (2)

RomanPerekhrest
RomanPerekhrest

Reputation: 92884

Alternative awk approach:

awk -F' *|@' '$0~/^@/ && NF==2{fn=$2;next}{ print > fn }' one
  • -F' *|@' - compound field separator (space(s) or @)

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 204218

awk 'NF==1 && sub(/^@/,""){f=$0; next} {print > f}' one

If you will have many output files and don't use GNU awk then you'll have to close them as you go:

awk 'NF==1 && sub(/^@/,""){close(f); f=$0; next} {print > f}' one

Upvotes: 2

Related Questions