Re: 2048 character limit
by Zaxo (Archbishop) on Jun 05, 2002 at 22:39 UTC
|
You can open both files in Perl, which has no line length limits:
{
my ( $in, $out );
open $in, '<', $filepath or die $!;
open $out, '>', $newpath or die $!;
while (<$in>) {
# process $_ which is the current line
$_ = quux($_);
print $out; # assumes $_ has everything to print now
}
close $out or die $!;
close $in;
}
There are lots of ways to do this, many shortcuts. I showed this one because it's explicit about what it does. The use of lexical $in, $out as filehandles is relatively new, as is the three-argument open. For older perl, use globs like \*IN, \*OUT for filehandles, and concatenate the direction arrows '<', '>' with a space and the file path.
Update: After a lot of heat and little light, it seems that what you want is getgrent. If it fails, your /etc/group file is probably broken. See also 'perrldoc User::grent'.
After Compline, Zaxo
| [reply] [d/l] |
Re: 2048 character limit
by scain (Curate) on Jun 05, 2002 at 22:46 UTC
|
druid,
Along the lines of Ryszard's solution, but it puts the print
inside the while, like Beatnik suggested, and breaks long lines:
while (<FH>) {
my $templine = $_;
while (length $templine > 60) {
my $substring = substr ($templine, 0, 60);
print "$substring\n";
$templine = substr ($templine, 61);
}
print "$templine\n";
}
Note that this is untested code, and I am notorius for
screwing up indexes in things like substr, so be sure to
check for off by one errors.
Scott
Please see my resume;
I am looking for a Bioinformatics Scientist position. | [reply] [d/l] |
|
while (<>) {
print "$1\n" while s/^(.{60})//;
print "$_\n" if $_;
}
____________ Makeshifts last the longest. | [reply] [d/l] |
|
| [reply] |
|
|
doesnt work.. i just get the same line over and over .. ill try and discover the prob..
| [reply] |
Re: 2048 character limit
by Ryszard (Priest) on Jun 05, 2002 at 22:27 UTC
|
Try this untested snippet:
#!/usr/bin/perl -w
use strict;
my $file = '\/etc\/group';
local*FH;
open(FH, $file) || die "Cannot open file: $!";
my @contents = <FH>;
close FH;
print $_ foreach (@contents);
| [reply] [d/l] |
|
You can stick a print; inside a while(<FH>) { } to avoid stuffing the memory with file contents...
Greetz
Beatnik
... Quidquid perl dictum sit, altum viditur.
| [reply] [d/l] [select] |
|
Tried this one .. and i get the first part of the group file that i am matching for if (/steel/) i get
steel:!:1:
but not the list of users in the group .. basically same problem I personally have because this code is the same as what i wrote
| [reply] |
|
| [reply] |
|
|
#!/usr/bin/perl -w
open (FILE,"/home/druid/group");
open (FILE2,">/home/druid/t");
while(<FILE>){
if (/steel/){
print FILE2;
}
}
close FILE;
close FILE2;
| [reply] |
|
Try running this:
| | #!/usr/bin/perl -w
use strict;
open my $fh_group, "</etc/group" or die "Cannot open group file";
while (<$fh_group>) {
chomp;
my ($grp, $pwd, $gid, $users) = split /:/;
if ($grp eq $ARGV[0]) {
my @users = split /,/, $users;
print join $/, @users;
}
}
|
Save it to a file, and run it with a command line argument of the group you'd like to check membership for, for example:
perl program.pl steel
MeowChow
s aamecha.s a..a\u$&owag.print | [reply] [d/l] [select] |
Re: 2048 character limit
by Abigail-II (Bishop) on Jun 06, 2002 at 11:48 UTC
|
#!/usr/bin/perl
use strict;
use warnings 'all';
setgrent;
while (my ($name, $passwd, $gid, $members) = getgrent) {
print "$gid: $name: $members\n";
}
endgrent;
__END__
The setgrent and endgrent are
optional.
Abigail
| [reply] [d/l] [select] |
Re: 2048 character limit
by robobunny (Friar) on Jun 06, 2002 at 17:17 UTC
|
this doesn't have anything to do with perl, but note that you can have multiple lines for the same group in an /etc/group file. ie, these two are equivilent:
Case 1:
admin::111:bob,joe,fred
Case 2:
admin::111:bob,joe
admin::111:fred
of course, that is only if you are editting your group file by hand. also note that there is typically a limit to the number of users that you can have in a single group. | [reply] |