Split file upon increments of string value
I have a large file that contains thousand of records. Each record begins with a specific string. I'd like to split the file into many smaller files but not one output file per record, maybe 5 or 10, or whatever.
I'm using this right now to split the file: Code:
awk '/STRING/{n++}{print >"out" n ".txt" }' input_file.txt How can I have awk split the file at every 10th instance of "STRING"? I tried adding an NR variable, but that was a mess. Note, the records aren't the same size, so I can't just split based on number of lines. |
Try to compute the output file name based on the value of n, i.e.
Code:
awk '/STRING/{n++} n%5{file = sprintf("out%03d.txt",n/5+1)}{print > file }' input_file.txt In addition I used the condition Code:
n % 5 |
would the split command work ?
|
Thanks colucix, it worked perfectly! I never would have figured that out.
|
schneidz, as I understand the man page for split, I can only split into files of equal size (bytes or lines). If my records were of equal length I would have used that. Split was my first thought too.
|
Quote:
|
Quote:
My file contains lots of records where each record is a different length. One record might be 5 lines but the next could be 17, or 85, etc. Using Code:
split -l 5 file.txt prefix |
All times are GMT -5. The time now is 02:14 AM. |