Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

2048 character limit

by druid (Initiate)
on Jun 05, 2002 at 22:21 UTC ( [id://172026]=perlquestion: print w/replies, xml ) Need Help??

druid has asked for the wisdom of the Perl Monks concerning the following question:

Hello all, First time poster. I am trying to get a line out of /etc/group and finding that due to a vi limitation of 2048 characters (I think) I cant seem to print the entire line to a file. I have tried open, @=`cat ` both to no effect. Please let me know if you guys have a decent solution to my problem. R.

Replies are listed 'Best First'.
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

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.

      Or much simpler:
      while (<>) { print "$1\n" while s/^(.{60})//; print "$_\n" if $_; }
      ____________
      Makeshifts last the longest.
        doesnt work ..
      doesnt work.. i just get the same line over and over .. ill try and discover the prob..
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);
      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.
      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
        Ahhhh, so you problem isnt reading the file, but matching strings within the file.

        I reckon if you post your code, someone may come up with a solution.

      #!/usr/bin/perl -w open (FILE,"/home/druid/group"); open (FILE2,">/home/druid/t"); while(<FILE>){ if (/steel/){ print FILE2; } } close FILE; close FILE2;
        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
Re: 2048 character limit
by Abigail-II (Bishop) on Jun 06, 2002 at 11:48 UTC
    I wonder what vi has to do with it. Next time, please show some code, and indicate what went wrong (did you get an error message? Unexpected results?)

    But you might want to use *grent:

    #!/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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://172026]
Approved by scain
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-04-16 16:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found