Reputation: 147
I have a file as follows
File name split.txt
file part alpha
abc
def
hij
file part beta
klm
nop
file part charlie
qrs
tuv
wxy
zzz
The file needs to be delimited on the "file part" line and renamed according to last word in the file part line. i.e. after the split, I should have three files as follows:
File 1 - file name is alpha.txt
abc
def
hij
File 2 - file name is beta.txt
klm
nop
File 3 - file name is charlie.txt
qrs
tuv
wxy
zzz
Thus far, I have managed to split the file using the following command
awk '/file part/{n++}{print >"file" n ".txt"}' split.txt
This results in the file names being file1.txt, file2.txt and file3.txt.
How do I get the file names to be alpha.txt, beta.txt and charlie.txt?
Upvotes: 0
Views: 522
Reputation: 195039
This one-liner should help:
awk '/file part/{fn=$NF ".txt"}{print > fn}' split.txt
The idea is same as your codes, just change the sequence number by the last word in the line as filename.
Upvotes: 4