Hi jet speed, On Friday 15 Apr 2011 00:23:17 jet speed wrote: > Hi, > > I need help in formatting ouput from system command, i could'nt figure out > a way to format output from system command. appreciate your help with > this, the details are below. > > hlis3 is file with list of clients > > hosta > hostb > hostc > hostd > > The below program looks through each client and outputs the class name, > > the desired output needs to be in the below format > > ------------------------------------------------------------------------- > hosta Solaris_DEV > > hostb Solaris_DEV > > hostc Archivelogs__DV > Archivelogs__DV > Solaris_DEV > > hostd Archivelogs__DV > > --------------------------------------------------------------------------- > > #!/usr/bin/perl > > > > use strict; > use warnings; > > open (FILEOUT, ">>cmdout") ||die "cant open cmdout: $! \n"; > select (FILEOUT);
1. Don't use bareword file-handles. 2. Use the three-args open: open (my $file_our, '>>', 'cmdout') or die "Cannot open cmdout: $!"; 3. You shouldn't use select here. > > > open(FILE, "hlis3") || die "Can't open hlis3: $!\n"; > See above. Maybe you'd like to read "Modern Perl" here: http://perl-begin.org/books/#modern-perl > while (<FILE>) { > Put the line in an explicit variable: while (my $line = <$fh>) { Also don't call variable names file: http://perl-begin.org/tutorials/bad-elements/#calling-variables-file > chomp; > > print; > You've just printed it without the chomp. > > my $cmd = `bppllist -byclient $_ \|grep \"CLASS \" |awk \'\{print \$2\}\' > `; > 1. Why are you using grep and awk from within Perl, which can do all they can do and more? 2. By using $_ inside the `...` you're opening yourself to a lot of shell- script-code injection problems: * http://shlomif-tech.livejournal.com/35301.html * http://shlomif-tech.livejournal.com/14671.html 3. You are backslash-escaping many characters that do not need to be backslash escaped inside a `...`. 4. The variable called $cmd is misleading because it's not a command, but its output. Regards, Shlomi Fish -- ----------------------------------------------------------------- Shlomi Fish http://www.shlomifish.org/ Chuck Norris/etc. Facts - http://www.shlomifish.org/humour/bits/facts/ <rindolf> I am not solvable. I am Turing hard. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/