Solved

perl parsing file - conditionally splitting to new file(s)

Posted on 2013-12-15
12
227 Views
Last Modified: 2013-12-15
I have a text file which is as follows


000001      $$1orange$$2apple2$$3pear
000002      $$1orange$$2apple2$$3pear
000003      $$1orange$$2apple2$$3pear
000002      $$1pear$$2apple2$$3orange
000003      $$1elephant$$2tiger$$3giraffe
000003      $$1elephant$$2tiger$$lion
000003      $$1elephant$$2monkey$$lion


The first column (i.e. 000001) is the record ID.  The subfields are then denoted with the $$.  So, in the first line, subfield1 is orange, subfield2 is apple, subfield3 is pear

I'm wanting (and the file could be a few hundred lines or just a couple) to split the file based on the record ID, and subfield 2.

So, the first time it encounters a unique ID/subfield 2 pair (e.g. 000001 / apple) it should write out to one.  The second (and subsequent) times it finds this combo, it should write out to a different file.

Also, if it finds a repeat of the ID, but subfield 2 is different - each occurrence of this should write to a separate file.

For example, the file above would be parsed into the following files:

File1 (the first occurrence of ID/Subfield2 being seen):

000001      $$1orange$$2apple2$$3pea
000002      $$1orange$$2apple2$$3pear

File2 (second occurrence of an ID being seen, but subfield2 is different):
000003      $$1elephant$$2tiger$$3giraffe

File3 (third occurrence of an ID being seen, but subfield2 is different):
000003      $$1elephant$$2monkey$$3giraffe

LastFile (2nd occurrence of ID/Subfield2 being seen):
000003      $$1orange$$2apple2$$3pear
000002      $$1pear$$2apple2$$3orange
000003      $$1elephant$$2tiger$$lion

How can this be achieved in perl?

any help much appreciated (as always!)
0
Comment
Question by:yelbow
  • 7
  • 5
12 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 39719995
perl -F'/\s*\$\$/' -ape '$f=++$f{"@F[0,2]"};open STDOUT,">>$f" or warn "$f $!"' file
0
 

Author Comment

by:yelbow
ID: 39720023
Hi Ozo,

Thanks for this - however, it doesn't quite work (and looking back my example may have been a little dodgy)

It gives me the following five files:

'000001$$1orange'
000001      $$1orange$$2apple2$$3pear

cat '000002$$1orange'
000002     $$1orange$$2apple2$$3pear

cat '000003$$1orange'
000003      $$1orange$$2apple2$$3pear

cat '000002$$1pear'
000002      $$1pear$$2apple2$$3orange

cat '000003$$1elephant'
000003      $$1elephant$$2tiger$$3giraffe
000003      $$1elephant$$2tiger$$lion
000003      $$1elephant$$2monkey$$lion

The output of the first 3 files should all be combined in 1, as it's the first time each of them has been seen.

The fourth file is fine, as it's the second time the ID 000002 has appeared, but subfield 2 is different.

The 5th file has "000003 / tiger" which hasn't been seen before, so should be in the first file etc
0
 
LVL 84

Expert Comment

by:ozo
ID: 39720030
I think http:#a39719995 is now corrected, but I'm still a little confused about the example.
0
 

Author Comment

by:yelbow
ID: 39720049
Hi,

That now gives:
cat 2
000002      $$1pear$$2apple2$$3orange
000003      $$1elephant$$2tiger$$lion
cat 1
000001      $$1orange$$2apple2$$3pear
000002      $$1orange$$2apple2$$3pear
000003      $$1orange$$2apple2$$3pear
000003      $$1elephant$$2tiger$$3giraffe
000003      $$1elephant$$2monkey$$lion

which isn't quite right.  You're absolutely right about the example though - so, I'll  try that again (sorry!)

Original File:
000001      $$1orange$$2apple2$$3pear
000002      $$1orange$$2apple2$$3pear
000003      $$1orange$$2apple2$$3pear
000002      $$1pear$$2apple2$$3orange
000003      $$1elephant$$2tiger$$3giraffe
000003      $$1elephant$$2tiger$$3lion
000003      $$1elephant$$2monkey$$3lion
000003      $$1lion$$2monkey$$3giraffe

Resulting files:

File 1 (the first occurrence of each pair of ID/subfield2):
000001      $$1orange$$2apple2$$3pear
000002      $$1orange$$2apple2$$3pear
000003      $$1orange$$2apple2$$3pear


File 2 (second occurrence of an ID being seen, but subfield2 is different)
000003      $$1elephant$$2tiger$$3giraffe

File 3 (third occurrence of an ID being seen, but subfield2 is different)
000003      $$1elephant$$2monkey$$lion

File 4 (the second (and subsequent) occurences of an ID/subfield2 pair)
000002      $$1pear$$2apple2$$3orange
000003      $$1elephant$$2tiger$$lion
000003      $$1lion$$2monkey$$3giraffe

Does that help?  Sorry if I'm being unclear.

The ultimate aim is to only have the ID appear once in each file, unless it's in the final file which is a catchall of the second and subsequent occurrences of the ID/Subfield2 pairings.
0
 
LVL 84

Expert Comment

by:ozo
ID: 39720101
Now it sounds like you are describing something like
perl -F'/\s*\$\$/' -ape '$i=++$i{$F[0]};$f=++$f{"@F[0,2]"};open STDOUT,">>$i.$f" or warn "$i.$f $!"'
But I'm still confused
00003      $$1elephant$$2tiger$$3giraffe is the first occurrence of the pair 000003/2tiger, so why isn't it in the first file?
0
 

Author Comment

by:yelbow
ID: 39720116
Hi,

It's because it's the second occurrence of the ID 00003 - and in all files bar the "catchall" file (where its seen both the id and subfield2 before), the ID can only exist once.

I'm, roughly, trying to achieve this:

For each line
	if I've seen this ID before then
		if I've seen this ID/Subfield2 combo before then
			write out to a single catchall file
		else
			write out to new filex
		endif
	else
		Write out to file1
	endif
end for

Open in new window

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 84

Expert Comment

by:ozo
ID: 39720124
Here's another interpretation:
perl -F'/\s*\$\$/' -ape '$f=$f{"@F[0,2]"}++||$F[2];open STDOUT,">>$f" or warn "$f $!"'
0
 

Author Comment

by:yelbow
ID: 39720139
Ooh, that's nearly there (thanks so much for your patience), only it writes out *all* of the lines to the "catchall" file?
0
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 39720141
This should be a literal translation of http:#a39720116
perl -F'/\s*\$\$/' -ape '$f=$f{$F[0]}++?$f{"@F[0,2]"}++?"catchcall":++$n:"file1";open STDOUT,">>$f" or warn "$f $!"'
0
 
LVL 84

Expert Comment

by:ozo
ID: 39720164
Are you sure catchall file and new file aren't switched in  http:#a39720116?
It matches your most recent example better when they are switched.
0
 

Author Closing Comment

by:yelbow
ID: 39720167
That seems perfect, thanks so much for your help and patience
0
 
LVL 84

Expert Comment

by:ozo
ID: 39720170
perl -F'/\s*\$\$/' -ape '$s=$f{"@F[0,2]"}++;$f=$f{$F[0]}++?$s?"catchcall":++$n:"file1";open STDOUT,">>$f" or warn "$f $!"'
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now