need help with awk

I have a CSV file like this

#product
laptop,notebook,ipad
hp,acer,apple
dell,lenovo,sony

#region
city,country
newyork,US

#Time
month,year
jan,2011
feb,2012

---------------------------------
I want to store this data seperately in two files with different names:

First file 1.file_prod_reg should have data under #product and #region

#product
laptop,notebook,ipad
hp,acer,apple
dell,lenovo,sony

#region
city,country
newyork,US

Second file 2.file_time which should contain data under time

#time
month,year
jan,2011
feb,2012

PLease help mt out on this i tried using awk but no luck :wall:

Thanks,
Mani

What you have tried so far ..

Hi Diddy,

u can try something like this:

bash-2.03$ cat brk.sh
n=`grep -n "#Time" rough1.txt|awk -F":" '{print $1 }'`
m=`expr $n - 1 `
sed -n '1,'$m'p' rough1.txt > file1.file_prod_reg
sed -n ''$n',$p' rough1.txt > file2.file_prod_reg
1 Like

Thanks,

but this logic wont work if the #Time headind repeats again..

i want to direct specific data under specific heading to a separate specific file... will that be possible?

Hi Diddy,

Can you give example of what you are looking for.

see the file is like this

#heading1
Records

#heading2
Records 

#heading3
Records

i want to direct #heading1 and its records into a separate file, #heading2 and its records into another file..like this ..so on....The original file is a CSV file,

---------- Post updated 11-25-11 at 04:41 PM ---------- Previous update was 11-24-11 at 09:47 PM ----------

some one help me out on this.

awk '/^#/{++c}NF{file="file"c".file_prod_reg";print $0 > file}' file
1 Like

what if i have to direct #heading1 and #heading2 records into a one single file
and #heading3 into another file?

The #heading1 and #heading2 will repeat many times within the original file.. thats why i need those records of #heading1 and #heading2 in a single file..

awk '/^#/{file="file"(/#[product|region]/)?1:2".txt"}{print > file}' file