LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   split file having similar tags (https://www.linuxquestions.org/questions/linux-newbie-8/split-file-having-similar-tags-4175418480/)

Alkass 07-24-2012 07:10 PM

split file having similar tags
 
Hi

I have a file which holds records like
Code:

<record>
line1
line2
line3
</record>

<record>
line1
line2
line3
</record>

in other words, I do have X <record> tags.. So, how can I split the initial file into two or N files each one having X/N <record> inside ? For example if I do have a file with 100 <record>, and I want two files, 1rst file should have 1->50 2nd from 51->100 for example


Thanks in advance

Alex

Tinkster 07-24-2012 08:30 PM

Something like this might work ... :) if the records are separated by a blank line.

Code:

awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n";count=1}{print $0 > FILENAME"."count; if(NR%3==0){count++}}' file

That sample will chop 3 records and place them into a file each lot

bsat 07-25-2012 01:50 AM

See if this helps
Code:

awk 'BEGIN{RS="</record>";count=1;i=1}  {{print $0"</record>">>"file"i;count++;if(count>2){i++;count=1}}} END{system("rm file"i" ")}' input
The script splits the input file into multiple files with 2 records per file. You can increase the number of records per file by changing the number in the if condition.
Each new file is named, file1,file2..etc.


All times are GMT -5. The time now is 05:30 AM.