-1

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:

  • edit my html, js, css and docs in the one file (the files aren't large)
  • m4 preprocesses the one file
  • splitit reads m4 output and writes out several files
  • indent/beautify the files
  • recombine into fewer files with cat...
effbiae
  • 1,087
  • 1
  • 7
  • 22
  • 2
    Are you telling us you want to create a tool named `splitit` with that functionality or advertising one that you have created? If the former - how can we help you create a tool to beautify a files contents when we don't know what that content is other than a line like `-- header --`? – Ed Morton Jul 15 '17 at 12:07
  • @EdMorton actually, I did write a tool to do this in c a few years ago, but I'm sure it can be done in awk pretty easily - I'm not an awk expert. I don't need a beautifier - those exist. Just need splitit. – effbiae Jul 15 '17 at 12:09
  • 1
    Then get rid of all the requirements that don't apply and just list what you need help with so no-one wastes their time trying to do something else.. Also include what you've tried so far. See [ask] if that's not clear. – Ed Morton Jul 15 '17 at 12:18

2 Answers2

2
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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • $ awk -F'[@[:space:]]' 'NF==2{f=$2; next} {print > f}' one awk: cannot open "" for output (No such file or directory) – effbiae Jul 15 '17 at 12:30
  • Then your input file doesn't look like the one you posted. Run it against the input file you posted and it will work. I modified the command btw - use the updated version but that's unrelated to the error you got. – Ed Morton Jul 15 '17 at 12:32
  • 1
    Thank you, your splitit worked (I tried the first one). – effbiae Jul 15 '17 at 12:38
0

Alternative awk approach:

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