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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help seperating and parsing a text file input

Status
Not open for further replies.

DMG2000

Programmer
Aug 13, 2000
52
0
0
US
Hi:
I am new to perl and cgi, atho am experienced with vb and asp, so bear with me:

I need to take a text file generated from a db of users@domains and I need to accomplish two things:
1>
Seperate the users@domains so that output is like
domain1:user1,user2,user3
domain2:user1,user2,user3
and 2>
Seperately parse the text file into line number variables for each address and group by 5 lines like
@group1
var1 = user1
var2 = user2
var3 = user3
var4 = user4
var5 = var5
@group2
var6 = user6
var7 = user7
so forth indefine till eof

Is there any one who can help me with this please? I have a feeling its something super simple. Thanks!



Shawn (DMG2000)
compute_x@yahoo.com
 
Code:
open(IPF,&quot;<inputFileHere&quot;) or die &quot;Failed to open input file, $!\n&quot;;
while ($line = <IPF>)
{
# Given this text pattern for input....
# $line = 'domain1:user1,user2,user3';

# use split to get at the pieces.
@parts = split(/:/,$line);
# @parts now looks like ('domain1','user1,user2,user3')

# keep a list of domains
$domain = $parts[0];
push @domains, $domain;

# name your array of users by the domain to which they belong
# this assumes your domain names are unique (non-repeating)
@$domain = split(/,/,$parts[1]);
# @$domain now looks like ('user1','user2','user3')
}

$domain = '';
# foreach domain found, list the users
foreach $domain (@domains)
    {
    foreach $user (@$domain) { print &quot;$domain - $usera\n&quot;; }
    }

I have not run this, so there may be a typo. I hope this illustrates some of what you are trying to do. I not, we'll try again.

'hope this helps.




keep the rudder amid ship and beware the odd typo
 
ok...my input reads as follows:
mark@sales
ken@sales
kelly@sales
bob@accounting
kim@accounting

ect....

what I need to do is parse so that I get an output like:
sales:
mark
ken
kelly

accounting:
bob
kim

I have tried the code above, and even tried tweaking, but get bomb errors from perl....

I really really appreciate your help!

Shawn (DMG2000)
compute_x@yahoo.com
 
This runs on my machine as is and produces that output you're looking for. Feel free to ask questions or make comments. It is a little verbose, to lend clarity. I hope this helps.

Code:
#!/perl
# open and read input file (mine was 'input.lst')
open(IPF,&quot;<input.lst&quot;) or  die &quot;Failed to open input.lst, #!\n&quot;;
while ($line = <IPF>)
    {
    ($name,$section) = split(/@/,$line);
    chomp($section);

    # in case you have several sections, 
    # catch the name of each new section
    # the next line catches a unique of each section
    $sections{$section} = 1;

    # build a list (array) for each section
    # name the array for the section
    push @$section,$name;	
    }

# foreach unique section in the hash, '%sections'
foreach $set (keys(%sections))
    {
    print &quot;\n$set:\n&quot;;
    foreach $person (@$set)
	    {
	    print &quot;$person\n&quot;;
	    }
    }




keep the rudder amid ship and beware the odd typo
 
Oh man, you are beautiful!!!! Works exactly as I want it to, thank you much. I will tweak around with the code and post back if I have any further questions, again, I appreciate it!

Shawn (DMG2000)
compute_x@yahoo.com
 
Perhaps you could help with the following as well, this should be an easy one for you:

I want to take a text file containing user names that appear as such:
user1
user2
user3
ect...

I want to take each line into an array (I have this part already) and the delete the lines from the text file, 5 at a time...I need the delete processes....I'm sure its something super easy, I just don't know how to delete the whole line.

Thanks!

Shawn (DMG2000)
compute_x@yahoo.com
 
the easiest way to do this is to put the lines into an array, use splice to delete the first 5 then re-write the file.

open (FILE, &quot;text.txt&quot;);
@file = <FILE>;
close(FILE);

@delete = splice(@file, 0, 5);

open (NEWFILE, &quot;>text.txt&quot;);
foreach(@file)
{
print NEWFILE &quot;$_\n&quot;;
}
close(NEWFILE);
 
> open (FILE, &quot;text.txt&quot;);
> @file = <FILE>;
> close(FILE);

Now when this dumps the contents of FILE into an array and closes the file, does it delete the contents? When I write back to the file, will it insert duplicates, or start fresh?
This would be excellent if I could achive the above (starting the file fresh).

Thanks for your help!

Shawn (DMG2000)
compute_x@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top