Extract same text from file.

R

Ryan Keeling

Hi
I have the following text file which I want to use to populate text
files containing data only relevant to a particular project. Is it
possible for me to group similar lines together and put them in their
own text file? Such as a file containing all entries for
MS102_PCP.Implementation and another file for MS102_PCP.Support.

Any help would be greatly appreciated!


2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
2004:01:09:5:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:1.0
2004:01:20:2:04:MS102_PCP:Belfast:Charge:pCP:Standards:6.5
2004:01:19:1:04:MS102_PCP:Belfast:Charge:pCP:Standard:7.5
2004:02:03:2:06:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:03:03:3:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Support:+:2.5
2004:03:26:5:13:MS102_PCP:Belfast:Charge:pCP:Support:+:7.5
2004:03:29:1:14:MS102_PCP:Belfast:Charge:pCP:Support:+:8.0
2004:03:30:2:14:MS102_PCP:Belfast:Charge:pCP:Implementation:+:8.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:project-Control:+:7.5
2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:2.0
2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:0.7


Ryan
 
J

Josef Moellers

Ryan said:
Hi
I have the following text file which I want to use to populate text
files containing data only relevant to a particular project. Is it
possible for me to group similar lines together and put them in their
own text file? Such as a file containing all entries for
MS102_PCP.Implementation and another file for MS102_PCP.Support.

Any help would be greatly appreciated!


2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
2004:01:09:5:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:1.0
2004:01:20:2:04:MS102_PCP:Belfast:Charge:pCP:Standards:6.5
2004:01:19:1:04:MS102_PCP:Belfast:Charge:pCP:Standard:7.5
2004:02:03:2:06:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:03:03:3:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Support:+:2.5
2004:03:26:5:13:MS102_PCP:Belfast:Charge:pCP:Support:+:7.5
2004:03:29:1:14:MS102_PCP:Belfast:Charge:pCP:Support:+:8.0
2004:03:30:2:14:MS102_PCP:Belfast:Charge:pCP:Implementation:+:8.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:project-Control:+:7.5
2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:2.0
2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:0.7

Answer to your question: yes.

Obvious counter-question: what have you tried so far and where have you
had problems?

Since your data is reasonably structured, I'd use split and then create
a filename from fields 8 and 9 to append the current line to. If the
data isn't too much, you can use a hash of arrays to collect the data
and write it out in one go.
 
T

Tore Aursand

I have the following text file which I want to use to populate text
files containing data only relevant to a particular project. Is it
possible for me to group similar lines together and put them in their
own text file? Such as a file containing all entries for
MS102_PCP.Implementation and another file for MS102_PCP.Support.

2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
[...]

Yes, it is _very_ possible. What have you tried so far? You want to look
into 'perldoc -f split', for example.
 
A

Abhinav

Ryan said:
Hi
I have the following text file which I want to use to populate text
files containing data only relevant to a particular project. Is it
possible for me to group similar lines together and put them in their
own text file? Such as a file containing all entries for
MS102_PCP.Implementation and another file for MS102_PCP.Support.

Any help would be greatly appreciated!


2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
2004:01:09:5:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:1.0
2004:01:20:2:04:MS102_PCP:Belfast:Charge:pCP:Standards:6.5
2004:01:19:1:04:MS102_PCP:Belfast:Charge:pCP:Standard:7.5
2004:02:03:2:06:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:03:03:3:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Support:+:2.5
2004:03:26:5:13:MS102_PCP:Belfast:Charge:pCP:Support:+:7.5
2004:03:29:1:14:MS102_PCP:Belfast:Charge:pCP:Support:+:8.0
2004:03:30:2:14:MS102_PCP:Belfast:Charge:pCP:Implementation:+:8.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:project-Control:+:7.5
2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:2.0
2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:0.7

Something like this (untested) ?

use strict;
use warnings;

while (<STDIN>)
{
my @outStr = split ':';
system qq(echo '$_' >> '$outStr[9].txt');

}
Regards
 
A

Abhinav

Abhinav said:
Ryan Keeling wrote:

Hi
I have the following text file which I want to use to populate text
files containing data only relevant to a particular project. Is it
possible for me to group similar lines together and put them in their
own text file? Such as a file containing all entries for
MS102_PCP.Implementation and another file for MS102_PCP.Support.

Any help would be greatly appreciated!


2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:01:07:3:02:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
2004:01:09:5:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:1.0
2004:01:20:2:04:MS102_PCP:Belfast:Charge:pCP:Standards:6.5
2004:01:19:1:04:MS102_PCP:Belfast:Charge:pCP:Standard:7.5
2004:02:03:2:06:MS102_PCP:Belfast:Charge:pCP:Support:+:2.0
2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:5.5
2004:03:03:3:10:MS102_PCP:Belfast:Charge:pCP:Implementation:+:7.5
2004:03:02:2:10:MS102_PCP:Belfast:Charge:pCP:Support:+:2.5
2004:03:26:5:13:MS102_PCP:Belfast:Charge:pCP:Support:+:7.5
2004:03:29:1:14:MS102_PCP:Belfast:Charge:pCP:Support:+:8.0
2004:03:30:2:14:MS102_PCP:Belfast:Charge:pCP:Implementation:+:8.0
2004:01:05:1:02:MS102_PCP:Belfast:Charge:pCP:project-Control:+:7.5
2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:2.0
2004:01:06:2:02:MS102_PCP:Belfast:Charge:pCP:Meetings:+:0.7


Something like this (untested) ?

use strict;
use warnings;

while (<STDIN>)
{
my @outStr = split ':';
system qq(echo '$_' >> '$outStr[9].txt');

Oops ..this should be
system qq(echo '$_' >> '$outStr[5]$outStr[9].txt');
 
J

Joe Smith

Ryan said:
Such as a file containing all entries for
MS102_PCP.Implementation and another file for MS102_PCP.Support.

Here's some help regarding multiple output files:

my %filehandles
while(<>){
($dept,$proj) = (...); # Something to pick out the right columns
unless (exists $filehandles{$proj}) {
open $filehandles{$proj}, '>>', "$dept.$proj" or warn
"Cannot append to $dept.$proj: $!\n";
}
print $filehandles{$proj} $_ or warn "Output error $proj: $!\n"
}

As for the part marked (...); that is an exercise for the reader.
-Joe
 
R

Ryan Keeling

Thanks guys this worked perfectly, it was a bit of a d'oh on my part
that I didnt think of it earlier!!

Thanks again

Ryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top