Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble parsing file 1

Status
Not open for further replies.

3inen

Technical User
May 26, 2005
51
US
#AG1 4
Jacob Christopher Joseph Ethan
#AG2 3
Madison2 Olivia Emma
#AG3 2
Samantha Michael

Hi! all. i have the above input fie with data grouped as group name and members in that group that are seprated by a tab.

i wan to split the above data into 3 separate files (the line with hash and the immediate next line with members).

also i want to have in that file, one member per row and not in columns.

for example as

AG3.txt
Samantha
Michael


thanks in advance
her is what i have tried sofar.


#!/usr/bin/perl -w
open(FILER1, "input.txt") || die "couldn't open the file\n";
my ($file, $line, $memb, $num);
my (@members);
use strict;
use diagnostics;
while(!eof(FILER1)){
$line=<FILER1>;
if($line=~/#/){
$line=~/#(AG\d+)\s .*\n/;
$file=$1;
open FH, ">$file" or die "Failed to create file";
$line = <FILER1>;
@members = split /\t/, $line;
foreach $memb ( @members ){
print FH "$memb\n";
}

close FH;
}

}
 
How about something like:
[tt]
open(FILER1, "<input.txt") or die "couldn't open the file";
while(<FILER1>)
{
if(/^#(AG\d+)\s/)
{
open FH,">$1" or die "Failed to create file";
my $members = <FILER1>;
$members=~s/\t/\n/g;
print FH $members;
close FH;
}
}
close FILER1;
[/tt]
 
Code:
#!/usr/bin/perl
open IN, "<input.txt";
while (<IN>) {
  if (/#/) {
    @filenames=split(/\s+/, $_);
    $file=$filenames[0].".txt";
    open FH,">$file";
  } else {
    @names=split;
    print FH join( "\n", @names);
    close FH;
  }
}
close IN;
HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
thanks to both of you. they are perfect.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top