The code you provided seems to have a few syntax errors (e.g.,
/usr/local/bin/perl needs the slashes to be escaped, and the regex beginning with
sh is never terminated), but I've tried to clean it up as well as I could. This program is not equivalent to your Unix grep/awk pipeline, but I've tried to keep the meaning of your Perl code approximately the same.
#!/usr/local/bin/perl
use strict;
use warnings;
open(OF,">outr") || die "File not opening outr: $!\n";
foreach (`ps -ef | grep jones` =~ /[^\n]*\n/g) {
next if m!/usr/local/bin/perl ps1$! or /sh -c ps -ef \| grep jones >
+ outr$/;
print OF;
}
close(OF);
Hopefully this does about what you want. A couple of points about it:
- Backticks are used to capture the output of ps rather than a system with output piped to a file.
- "Jones" is changed to "jones" in the grep, since I think that's what you want.
- The elements of the array in a foreach are automatically aliased to $_, so there's no need to do it manually.
- Similarly, $_ is used by default by regexes and print, so there's no need to specify it there, either.
-- Mike
--
just,my${.02}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.