Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

3
  • This may break on things like & occuring elsewhere in the input. Commented Oct 11, 2023 at 5:39
  • Regarding print $0 > ++c".xml" - 1) you don't need the $0 as that's what awk prints by default, 2) an unparenthesized expression on the right side of input or output redirection is undefined behavior and will give you a syntax error in some awks, 3) not closing the output files as you go will lead to a "too many open files" error from most awks when you pass the system limit. It should be print > (++c".xml"); close(c".xml") or better out=++c".xml"; print > out; close(out) to be portable and robust. Commented May 20, 2024 at 10:50
  • The output files will all start with a blank line, though, (the one after the & in the input) so you should probably add a sub(/^\n/,"") before the print, and they will end with a blank line (there's one before the & and at the end of the file in the input and ORS will add one by default) so you should set ORS to null and just use the one from the input as the terminating newline for each file, i.e. awk 'BEGIN{RS="&"; ORS=""}{out=++c".xml"; sub(/^\n/,""); print > out; close(out)}' file.xml. Obviously in GNU awk you could manipulate RS and RT to handle that instead. Commented May 20, 2024 at 10:55