CptNemo
CptNemo

Reputation: 6755

Split file with awk numerating output files with double digits

I have this text file

@
text text text

text text text
@
text text text
@
text text text text
text text text

text text text

which I can split in multiple files at @ with

awk '/@/{n++;close(filename)}{filename = "part" n ".txt"; print >filename }'  text.txt

Still I would like to improve the command so to avoid including @ as first line of each new file: I want the first line of each file to be directly text text text.

Also I have few problem with the numeration of the files. The new files follow this numeration: part1.txt, part2.txt, ... , part10.txt but this create a problem when ordering the file because part10.txt will be ordered before part2.txt. Is it possible to have files numerate with two digits part01.txt and part02.txt?

Upvotes: 1

Views: 89

Answers (2)

Jotne
Jotne

Reputation: 41446

No need to test more than once. Use next to skip the @ row.

awk '/^@$/{n++;close(filename);next} {print >sprintf("part%02d.txt", n)}' text.txt

Upvotes: 1

janos
janos

Reputation: 124648

Here you go:

awk '/@/{n++;close(filename)} !/@/{filename = sprintf("part%02d.txt", n); print >filename; }' text.txt

Or a bit cleaner, with stricter pattern matching and without duplicating the pattern:

awk '{ if ($0 == "@") {n++;close(filename)} else {filename = sprintf("part%02d.txt", n); print >filename; }}' text.txt

Upvotes: 1

Related Questions