Perl delete match loop
Greetings i'm running a command that captures a lot of lines to a variable.
$ELEMENT = `./my command`; print $ELEMENT; i need to delete lines that contain a certain string. At this point i think my output is one huge line. i tried saving itan array then iterate over the array a delete the match when it finds it but no luck. help please? Thanks |
Quote:
There are MANY examples that can be found online for reading an array, and grep'ing for patterns within the array elements. First hit for "loop through array elements in perl", in Google: https://www.perlmonks.org/?node_id=816177 First hit for "grep for string in perl": https://www.perlmonks.org/?node_id=1003945 |
ok my first step is running the command to capture the output to a scalar.
@COMMAND_OUTPUT = `./command`; this command consists of about 130 lines. Ideally i would like to ignore a subset of lines containing 'x' foreach $var (@COMMAND_OUTPUT){ if ($var =~ m/pattern/) { then ignore that string\n; } i think my initial capture is wrong because i expect 130 elements in that array but there are only 5 when i assign it to a scalar and print. I'm novice here. Thanks, $NUM=@COMMAND_OUTPUT; print "$NUM\n"; # this says 5 elements |
What I do when doing line-by-line processing:
Code:
open (FILEIN, $fwdmsg) || die "Can't open $fwdmsg"; ## $fwdmsg is the name of a file The script I borrowed from just reads every input line and writes it into an email for a spam report. Some lines are used to populate a log file... I usually don't load an array when reading from a command: Code:
foreach $line (`cat /home/scasey/somefile.txt`) { Afterthought: Are you counting the array before or after you try to delete elements from it...and what are you doing to "delete" from the array? |
for the loop section i have the following. Should this work if i want to just print everything other than whats in the EXCLUDE array ?
@EXCLUDE="successfully", "execution"; foreach $var (@ELEMENT){ if ($var !=~ m/@EXCLUDE/) { print $var; } } |
Quote:
Probably not. I'm pretty sure your syntax is not correct at Code:
!=~ |
Quote:
Code:
@EXCLUDE = ('successfully', 'execution'); |
I think there is a pre-written program for that, named egrep:
Code:
program_that_produces_many_lines | egrep -v -- '(successfully|execution)' |
how I do it:
Code:
while (<>) { Code:
command | script.pl > out.log |
All times are GMT -5. The time now is 01:07 AM. |