LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-24-2012, 07:10 PM   #1
Alkass
Member
 
Registered: Mar 2010
Posts: 47

Rep: Reputation: 0
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
 
Old 07-24-2012, 08:30 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
1 members found this post helpful.
Old 07-25-2012, 01:50 AM   #3
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How can I split a file, without using 'split'? szboardstretcher Linux - Software 11 05-20-2011 02:43 PM
eliminate data with similar tags fs11 Programming 11 04-29-2008 03:46 PM
How to split file , .. awk or split ERBRMN Linux - General 9 08-15-2006 12:02 AM
split files using awk (or similar) lgualteri Programming 1 06-13-2005 09:17 AM
similar to dpkg-split on rpm based systems? lil_becky Linux - Newbie 1 01-19-2005 11:53 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration