LinuxQuestions.org
Visit Jeremy's Blog.
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, 08: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, 09:30 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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, 02: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:43 PM
eliminate data with similar tags fs11 Programming 11 04-29-2008 04:46 PM
How to split file , .. awk or split ERBRMN Linux - General 9 08-15-2006 01:02 AM
split files using awk (or similar) lgualteri Programming 1 06-13-2005 10:17 AM
similar to dpkg-split on rpm based systems? lil_becky Linux - Newbie 1 01-20-2005 12:53 AM


All times are GMT -5. The time now is 05:07 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration