Could anyone help me splitting a big file into subfiles. The file is something similar to this:

1.SYSTEM = UNIX
2.variable = value

3.>ACTIVATED 1

4.ALL: FILE1
5.ALL:FILE2
6.ALL:FILE3
7.ALL:FILE4

8.>ACTIVATED 1
9. ################*
10.>>CONDITIONS
11.################

12.MODEL:
13.<one ExeSimpleName> : <one or more Flags> : [Options]

I need to split this file into many files such that first file has only one ALL:FILE1 and second has ALL:FILE2 and third has only ALL: FILE3 and fourth ALL:FILE4

The remaining contents should be the same. Can anyone please give me an idea of doing it in perl...

Immediate help would be appreciated as I am struggling with this from few days.

Thanks
Hari.

Since this will be very dependent on the exact data in the file there is little sense in trying to help with data that is something similar to the real data.

I just need the logic of doing it .The real data is very close to the data which i gave.

when you say "the remaining contents should be the same" do you mean, in file1 (ALL:FILE1) it should have just ALL:FILE1, AND the rest of the file (with the exception of ALL:FILE2, and ALL:FILE3, etc) or .... what?

I just need the logic of doing it .The real data is very close to the data which i gave.

Use a regular expression and a counter.

commented: Exactly +10

If i think the file which i gave is "file x" then the files after split say filex1 should contain :

1.SYSTEM = UNIX
2.variable = value

3.>ACTIVATED 1

4.ALL: FILE1

5.>ACTIVATED 1
6. ################*
7.>>CONDITIONS
8.################

9.MODEL:
10.<one ExeSimpleName> : <one or more Flags> : [Options]

Let me know if its still not clear for you.

Load the original file into array with push. Go through the array X number of times. Look for each line containing FILEX and skip the ones containing FILEY. Then write out the array to new files. Fairly inefficient, but it will work fine.

He said he wanted the logic but what I suspect he really wants is code. I prefer to write code for people that show effort.

Hey understood. I was GOING to actually write the code... but... I decided to just put in the concept... with no code. I wrote the actual code in my head on my walking to the train today - HOWEVER... I didn't actually write it or post it because of what you mention - haha. BTW like I said my approach is inefficient but it will work perfectly. I don't like multi-nested loops/fors/whiles but in this case it will work fine. I suspect he just has to do this ONCE and if so, effectiveness is more important than efficiency IMO.

I do appreciate your post.

Mike

He said he wanted the logic but what I suspect he really wants is code. I prefer to write code for people that show effort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.