Perl Output redirection

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1570 Views - Last Post: 07 December 2009 - 06:41 AM

#1 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Perl Output redirection

Posted 02 December 2009 - 03:49 PM

#!/usr/bin/perl

#########################################################################
#	   PURPOSE: Read file /etc/group and print users that belong to
# groups to a difrent txt files
#
#			   By Ian Zink
#########################################################################

open FILE, "<", "/etc/group";

while (<FILE>) {
print $_;

}


close FILE;



This program opens a file and then prints all the lines in the file to the screan.
I want to take the output and instead of sending it to the screan i want to send it to a difrent file.
How do i do this?
Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Perl Output redirection

#2 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 02 December 2009 - 04:06 PM

Ok i got that.
this is the code
#!/usr/bin/perl

#########################################################################
#	   PURPOSE: Read file /etc/group and print users that belong to
# groups to a difrent txt files
#
#			   By Ian Zink
#########################################################################

open groups, "<", "/etc/group";
open output, ">", "output.txt";

while (<groups>) {
print output $_;

}

close output;
close groups;



Now i have to do this

read the /etc/group file and prints the list of users that belong to each group into a text file called ~/groups.txt. For instance, for the line

c_teterwa::500:teterwa

the group name is c_teterwa and there is only one user in this group which is teterwa. So, your PERL script should produce the line

c_teterwa teterwa

in ~/groups.txt file. If a group in the /etc/group file has no user names associated with it, then your ~/groups.txt file will NOT have a line for that particular group.

I cant figure out how to sort the users of the groups
Can some one help?
Thanks
Was This Post Helpful? 0
  • +
  • -

#3 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl Output redirection

Posted 03 December 2009 - 04:27 AM

View Postizthrower, on 2 Dec, 2009 - 11:06 PM, said:

#!/usr/bin/perl

#########################################################################
#	   PURPOSE: Read file /etc/group and print users that belong to
# groups to a difrent txt files
#
#			   By Ian Zink
#########################################################################

open groups, "<", "/etc/group";
open output, ">", "output.txt";

while (<groups>) {
print output $_;

}

close output;
close groups;



A minor code critique, if you don't mind:
  • Good on you for using three-argument "open" instead of two-argument! It would be even better, though, if you also used lexical filehandles instead of glob-style handles (especially lower-case globs, since they could easily collide with other names):
    open my $groups, '<', '/etc/group';
    open my $output, '>', 'output.txt';
    
    while (<$groups>) {
    ...
    
    

  • Although this program is small enough that you can get away without them, it's never too early to start developing the habit of starting every Perl program with
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    
    strict and warnings will save you much heartache if you ever end up writing more than a couple dozen lines of Perl for a single purpose.

View Postizthrower, on 2 Dec, 2009 - 11:06 PM, said:

[code]
Now i have to do this

read the /etc/group file and prints the list of users that belong to each group into a text file called ~/groups.txt. For instance, for the line

c_teterwa::500:teterwa

the group name is c_teterwa and there is only one user in this group which is teterwa. So, your PERL script should produce the line

c_teterwa teterwa

in ~/groups.txt file. If a group in the /etc/group file has no user names associated with it, then your ~/groups.txt file will NOT have a line for that particular group.

I cant figure out how to sort the users of the groups
Can some one help?

If I were given that assignment, I would start by looking up the format of the group file (try "man 5 group"). The wording of your example suggests that all members of a single group will be on the same line, but, if you're not already familiar with the file format, it's an important detail to verify.

The example also looks like the fields on that line are separated by colons, so you'd want a function that will allow you to split the line into its component fields. Perl happens to have a function named "split" which does exactly that. You can read all about it at http://perldoc.perl....ions/split.html

If you need more help, let us know what you've tried/how far you've gotten and I'm happy to drop a few more clues to keep you pointed in the right direction.
Was This Post Helpful? 0
  • +
  • -

#4 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 03 December 2009 - 03:29 PM

Yea its prity much
i know that i got to use split functions but i dont understand
how to remove the groups with out users and i dont understand
how to get rid of the numbers bettween them eather with out using a
bunch of split functins
Was This Post Helpful? 0
  • +
  • -

#5 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 03 December 2009 - 03:48 PM

#!/usr/bin/perl

#########################################################################
#	   PURPOSE: Read file /etc/group and print users that belong to
# groups to a difrent txt files
#
#			   By Ian Zink
#########################################################################

open groups, "<", "/etc/group";
open output, ">", "output.txt";		 #opens files

while (<groups>){

@users = @groups;

@users = split(/:+/);

print output @users;

}

close output;
close groups;						   #closes files



Ok i can get the : removed but when ever i try to get the x out it puts the : back in
also i havent been able to figure out how to remove the numbers and the groups with no users
can some one help?
Thanks
Was This Post Helpful? 0
  • +
  • -

#6 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 03 December 2009 - 04:04 PM

ok so i got the split to remove the :x: in the middle but i cant get the : at the end to get removed.
So i have problems removing the numbers and the groups with out users and to remove the : at the end of the line.
Can some one help?
Thanks
Was This Post Helpful? 0
  • +
  • -

#7 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl Output redirection

Posted 04 December 2009 - 04:54 AM

View Postizthrower, on 3 Dec, 2009 - 11:04 PM, said:

ok so i got the split to remove the :x: in the middle but i cant get the : at the end to get removed.
So i have problems removing the numbers and the groups with out users and to remove the : at the end of the line.

split doesn't modify its arguments. It returns a list of substrings that result from splitting on the delimiter. For example, split ':', 'c_teterwa::500:teterwa' will return the list 'c_teterwa', '', '500', 'teterwa'. So, assuming you have your line of input data in $line, you would get the first and fourth fields from it with:
my @fields = split ':', $line;
my $first_field = $fields[0];
my $fourth_field = $fields[3];
# Or, alternately, if you don't want to count them:
my $last_field = $fields[-1];


Note that you probably want to split on just :, not :+. If you use :+, then the line "my_group::1:me" will only see three fields, since the :: will be treated as a single delimiter rather than as two distinct delimiters with an empty field between them.

Also, you forgot to add
use strict;
use warnings;

Was This Post Helpful? 0
  • +
  • -

#8 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 04 December 2009 - 10:24 AM

Where should i put the
use warnings
use strict
Was This Post Helpful? 0
  • +
  • -

#9 dsherohman  Icon User is offline

  • Perl Parson
  • member icon

Reputation: 226
  • View blog
  • Posts: 654
  • Joined: 29-March 09

Re: Perl Output redirection

Posted 05 December 2009 - 04:15 AM

View Postizthrower, on 4 Dec, 2009 - 05:24 PM, said:

Where should i put the
use warnings
use strict

Right after #!/usr/bin/perl. Personally, I'd put them before the "PURPOSE" block comment, but they'll work just as well after. The important part is just that they're above any executable lines so that they will be in force and checking for potential problems in all of the actual code.
Was This Post Helpful? 0
  • +
  • -

#10 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 05 December 2009 - 12:18 PM

ok
can i use pattern mattching in the program to remove the : and the x and the numbers?
would it be any easyer that using split?
Was This Post Helpful? 0
  • +
  • -

#11 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 05 December 2009 - 12:30 PM

@fields = split ':', $line;
my $first_field = $fields[0];
my $fourth_field = $fields[3];
# Or, alternately, if you don't want to count them:
my $last_field = $fields[-1];

im confused on how to use this.
can you help?
Was This Post Helpful? 0
  • +
  • -

#12 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 05 December 2009 - 12:49 PM

 
#!/usr/bin/perl

#use strict;
#use warnings;

#########################################################################
#	   PURPOSE: Read file /etc/group and print users that belong to
# groups to a difrent txt files
#
#			   By Ian Zink
#########################################################################

open groups, "<", "/etc/group";
open output, ">", "output.txt";		 #opens files

while (<groups>){

@users = @groups;

@users = split(/\:x:|:+/);

print output @users;

}


close output;
close groups;						   #closes files




ok with this code i can get to remove the :x: and the : at the end of each line.
i need help removing the numbers and replacing them with spaces and removing the groups without users.
can you help?
Was This Post Helpful? 0
  • +
  • -

#13 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 05 December 2009 - 12:56 PM

 
#!/usr/bin/perl

#use strict;
#use warnings;

#########################################################################
#	   PURPOSE: Read file /etc/group and print users that belong to
# groups to a difrent txt files
#
#			   By Ian Zink
#########################################################################

open groups, "<", "/etc/group";
open output, ">", "output.txt";		 #opens files

while (<groups>){

@users = @groups;

@users = split(/\:x:|:+|0+|1+|2+|3+|4+|5+|6+|7+|8+|9+ /);

print output @users;

}


close output;
close groups;						   #closes files




ok with this code i got to remove the :x: and the : at the end and the numbers.
Is there any easyer way to do this?
also im having trouble removing the groups with out users
Was This Post Helpful? 0
  • +
  • -

#14 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 05 December 2009 - 01:10 PM

 
#!/usr/bin/perl

#use strict;
#use warnings;

#########################################################################
#	   PURPOSE: Read file /etc/group and print users that belong to
# groups to a difrent txt files
#
#			   By Ian Zink
#########################################################################

open groups, "<", "/etc/group";
open output, ">", "output.txt";		 #opens files

while (<groups>){

@users = @groups;

@users = split(/\:x:|:+|0+|1+|2+|3+|4+|5+|6+|7+|8+|9+/);

@output = join("  " , @users);

print output @output;

}


close output;
close groups;						   #closes files




ok with this code i got to insert the space bettween the groups and users
still need help if i can make this easyer and how i can remove the the groups with out users?
also
when i print @output[1] it doesnt print anything
but
when i print @output[0] it prints all the elements
is that suposet to happen?
thanks
Was This Post Helpful? 0
  • +
  • -

#15 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Perl Output redirection

Posted 06 December 2009 - 03:01 PM

#!/usr/bin/perl

#use strict;
#use warnings;

#########################################################################
#	   PURPOSE: Read file /etc/group and print users that belong to
# groups to a difrent txt files
#
#			   By Ian Zink
#########################################################################

open groups, "<", "/etc/group";
open output, ">", "output.txt";		 #opens files

while (<groups>){

for ($count=0; $count<80; $count++ )
{

@groups = split(/\:x:|:+|0+|1+|2+|3+|4+|5+|6+|7+|8+|9+/);

@groups = join("  " , @groups);

@output = @groups[$count];

print output @output;
}
}


close output;
close groups;		   #closes files

 


ok with this code i got it to put each line as an element in the array and it allso takes out the the :x: and the : at the end
as well it takes off the numbers and puts spaces inbetween the groups and the users.
i still NEED HELP ON THE REMOVING GROUPS WITH OUT USERS!!
can some one plz help
thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2