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

For some reason this script doesnt always work on some unix systems. Any changes I could make to this to make it work better?
#!/usr/local/bin/perl open (INPUT, "< /etc/passwd"); while(<INPUT>) { ($col1, undef, undef, undef, $col5, undef) = split(/:/, $_); #undef are blank fields here write(); } format STDOUT_TOP = Name ID ---- -- . format STDOUT = @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<< $col1, $col5 .

Replies are listed 'Best First'.
Re: Formatting output
by cjf (Parson) on Jun 13, 2002 at 12:15 UTC

    chomp the input from /etc/passwd to remove the newlines, splitting into an array is much cleaner in my opinion. Checking your open calls and using strict and warnings is also good practice.

    #!/usr/bin/perl -w use strict; open INPUT, "</etc/passwd" or die "Can't open file #!\n"; my @lines; while (<INPUT>) { chomp; @lines = split /:/; write(); } format STDOUT_TOP = NAME ID ---- -- . format STDOUT = @<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<< $lines[0], $lines[4] .

    Also, what systems is it failing on, what errors does it give?.

      I need to find out what system it is on. Thanks for the input and I will try it out when the system is back up.
Re: Formatting output
by jmcnamara (Monsignor) on Jun 13, 2002 at 12:17 UTC

    It worked for me on a Linux and a Solaris system. What happens when it doesn't work.

    The following does something similar as a one liner:     perl -F: -ane 'printf "%-20.20s\t%-20.20s\n", $F[0], $F[4]' /etc/passwd

    Or following Zaxo's getpwent suggestion:     perl -e 'printf "%-14.14s\t%-14.14s\n", $a[0], $a[6] while @a = getpwent()'

    --
    John.

Re: Formatting output
by Zaxo (Archbishop) on Jun 13, 2002 at 12:36 UTC

    This has nothing to do with your output format question, but I'd suggest a look at getpwent. Output format can also be done with printf and sprintf, which may be more comfortable for C programmers.

    After Compline,
    Zaxo

Re: Formatting output
by Abigail-II (Bishop) on Jun 13, 2002 at 12:16 UTC
    Please define "doesnt always work". If I run the program, I do get output. Nothing strange happens. What do you expect as output, and what happens?

    Abigail