split file based on group count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split file based on group count
# 1  
Old 08-18-2010
split file based on group count

Hi,

can some one please help me to split the file based on groups.

like in the below scenario x indicates the begining of the group and the file should be split each with 2 groups below there are 10 groups it should create 5 files. could you please help?


Quote:
X ABCDE1
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW2
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
X ABCDE3
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW4
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
X ABCDE5
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW6
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
X ABCDE7
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW8
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
X ABCDE9
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW10
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
# 2  
Old 08-18-2010
Code:
awk '/^X/ {x+=1; if(x%2){z+=1}} {print >> "file_"z}' infile

# 3  
Old 08-18-2010
Post expected output according to the input file which you posted.
# 4  
Old 08-18-2010
Thanks for the replies!!!!

Zaxxon - getting the error message awk: 0602-575 Cannot find or open file file_.

expected output files would be 2 tables will be placed in each file and 5 different files would be created.

===========FILE1==================
X ABCDE1
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW2
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
============FILE2=================
X ABCDE3
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW4
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
============FILE3=================
X ABCDE5
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW6
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
============FILE4=================
X ABCDE7
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW8
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf
============FILE5=================
X ABCDE9
Z SSSSSSSSS
Z EEEEEEEEEE
Z QQQQQQQQ
X XYAW10
Y akldsf;dsaf
Y akjdsf;asdfj
Y akjfd;asfsaf

Last edited by hitmansilentass; 08-18-2010 at 12:45 PM..
# 5  
Old 08-18-2010
one > should be fine. Maybe that's the reason to get the error "0602-575 Cannot find or open file"

Code:
awk '/^X/ {x+=1; if(x%2){z+=1}} {print > "file" z}' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split files based on row delimiter count

I have a huge file (around 4-5 GB containing 20 million rows) which has text like: <EOFD>11<EOFD>22<EORD>2<EOFD>2222<EOFD>3333<EORD>3<EOFD>44<EOFD>55<EORD>66<EOFD>888<EOFD>9999<EORD> Actually above is an extracted file from a Sql Server with each field delimited by <EOFD> and each row ends... (8 Replies)
Discussion started by: amvip
8 Replies

2. Shell Programming and Scripting

awk - split data based on the count

Greetings Experts, I am generating a validation query through awk and facing an issue, which I need to overcome by splitting the data based on the pattern matching count in the value of an array. File1 -- Table11@column1@date@Table21@column1@varchar(10)@d;... (4 Replies)
Discussion started by: chill3chee
4 Replies

3. Shell Programming and Scripting

Split csv file by line count.

I have a very large csv file that I sort by the data that is in the second column. But what I need to do next is split the file in groups of say around 30,000 lines but don't split the data while there is still like data in the in the second column. Here is some of the data. ... (2 Replies)
Discussion started by: GroveTuckey
2 Replies

4. Shell Programming and Scripting

Split file based on records

I have to split a file based on number of lines and the below command works fine: split -l 2 Inputfile -d OutputfileMy input file contains header, detail and trailor info as below: H D D D D TMy split files for the above command contains: First File: H DSecond File: ... (11 Replies)
Discussion started by: Ajay Venkatesan
11 Replies

5. UNIX for Dummies Questions & Answers

Split binary file every occurrence of a group of characters

Hello I am new to scripts, codes, bash, terminal, etc. I apologize this my be very scattered because I frankly don't have any idea where to begin and I have had trouble sleeping lately. I have several 2GB files I wish to split. This Code 00 00 01 BA ** ** ** ** ** ** ** ** C3 F8 00 00 01 BB 00... (17 Replies)
Discussion started by: PatrickE
17 Replies

6. Shell Programming and Scripting

Split the file based on column

Hi, I have a file sample_1.txt (300k rows) which has data like below: * Also each record is around 64k bytes 11|1|abc|102553|125589|64k bytes of data 10|2|def|123452|123356|...... 13|2|geh|144351|121123|... 25|4|fgh|165250|118890|.. 14|1|abc|186149|116657|......... (6 Replies)
Discussion started by: sol_nov
6 Replies

7. Shell Programming and Scripting

KSH: Split String into smaller substrings based on count

KSH HP-SOL-Lin Cannot use xAWK I have several strings that are quite long and i want to break them down into smaller substrings. What I have String = "word1 word2 word3 word4 .....wordx" What I want String1="word1 word2" String2="word 3 word4" String3="word4 word5" Stringx="wordx... (5 Replies)
Discussion started by: nitrobass24
5 Replies

8. Shell Programming and Scripting

Split file by data group

Hi all, I'm having a little trouble solving a file split I need to get done. I have the following data: 1. Light 1A. Light Soft texture: it's soft color: the color value is that of something light vital statistics: srm: 23 og: 1.035 sp: 1.065 comment: this is nice if you like... (8 Replies)
Discussion started by: mkastin
8 Replies

9. Shell Programming and Scripting

Group and count file by date

Hi all, in BIN/SH I need to group and count files by date. Ie: ls -la -rw-r--r-- 1 aaa dba 122 Jul 13 14:28 as1.tmp -rw-r--r-- 1 aaa dba 122 Jul 13 15:27 as2.tmp -rw-r--r-- 1 aaa dba 122 Jul 21 17:04 as3.tmp -rw-r--r-- 1 aaa dba 122 Jul 23 15:45 as4.tmp -rw-r--r-- 1 aaa... (8 Replies)
Discussion started by: ric79
8 Replies

10. Shell Programming and Scripting

Split a file based on a pattern

Dear all, I have a large file which is composed of 8000 frames, what i would like to do is split the file into 8000 single files names file.pdb.1, file.pdb.2 etc etc each frame in the large file is seperated by a "ENDMDL" flag so my thinking is to use this flag a a point to split the files... (4 Replies)
Discussion started by: Mish_99
4 Replies
Login or Register to Ask a Question