i have seen similar questions all over but none seems close to what i'm trying to achieve.
I have a dynamic csv file (tab delimiter) that updates/gets appended each hour BUT NOTE: Only the number of rows underneath HEADER 1 and HEADER 2 increases every hour. Pls see two examples below as reference
Example of FileA.csv at the 3rd hour
HEADER 1 NUM
hour 1 5
hour 2 10
hour 3 15
HEADER 2 NUM
hour 1 3
hour 2 6
hour 3 9
HEADER 3 NUM
age 23
bus 21
pig 07
dog 40
Example of FileA.csv at the 7th hour
HEADER 1 NUM
hour 1 5
hour 2 10
hour 3 15
hour 4 20
hour 5 25
hour 6 30
hour 7 35
HEADER 2 NUM
hour 1 3
hour 2 6
hour 3 9
hour 4 12
hour 5 15
hour 6 18
hour 7 21
HEADER 3 NUM
age 13
bus 28
pig 85
dog 55
The rows underneath Header 1 and Header 2 increases each hour. Header 3 and below is the only thing that remains constant
So what i'm trying to achieve is simply separate FileA.csv into ABC.csv , DEF.csv , GHI.csv
using the 3rd hour example for reference to what i'm trying to achieve
ABC.csv
HEADER 1 NUM
hour 1 5
hour 2 10
hour 3 15
DEF.csv
HEADER 2 NUM
hour 1 3
hour 2 6
hour 3 9
GHI.csv
HEADER 3 NUM
age 23
bus 21
pig 07
dog 40
Below is what i tried to do using grep but i can't combine grep and cut to achieve this. I've tried using Sed but not sure how to cut and move it after searching. i know this can be achieved with awk but not strong in awk
- First cut out HEADER 3 and subsequent rows below and putting that into GHI.csv since that will always be constant, that way we are left with HEADER 1 and HEADER 2.
- Then cut out HEADER 2 and below by searching for the Header name and cutting it out with all subsequent rows beneath it
- Finally we are left with HEADER 1 which we either leave in FileA.csv or move it to ABC.csv
Pls Help. Thanks