LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl delete match loop (https://www.linuxquestions.org/questions/programming-9/perl-delete-match-loop-4175634275/)

bishop2001 07-17-2018 12:32 PM

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

TB0ne 07-17-2018 12:55 PM

Quote:

Originally Posted by bishop2001 (Post 5880330)
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?

Read the "Question Guidelines" link in my posting signature. We're happy to try to help you, but you need to provide details, and show your own efforts first. You don't post your program, give us examples of the input/output, or show us anything that you've done/tried to solve your problem.

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

bishop2001 07-17-2018 03:26 PM

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

scasey 07-17-2018 03:49 PM

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
while (my $line = <FILEIN>) {
        ...
        if ($line =~ "pattern") {
                ## act on matching line  maybe just skip it?
                ...
                next;
                ...
        }
        ...
}

That is, read each line, act on it according to the content of the line.
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`) {
...
if ($line =~ "pattern") {
                ## act on matching line  maybe just skip it?
                next;
                ...
        }
        ...
}

...but your load of the array looks right to me. Not sure about your counting step tho...no, that looks right too. So you need to look at the output of your command again, I think.

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?

bishop2001 07-17-2018 04:02 PM

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;
}
}

scasey 07-17-2018 04:13 PM

Quote:

Originally Posted by bishop2001 (Post 5880389)
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;
}
}

Does it? Did you try it?
Probably not. I'm pretty sure your syntax is not correct at
Code:

!=~
You need to read up on perl regular expressions and pattern matching. See my #4. signature link.

keefaz 07-17-2018 06:28 PM

Quote:

Originally Posted by bishop2001 (Post 5880389)
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;
}
}

Could be written as:
Code:

@EXCLUDE = ('successfully', 'execution');

$pattern = join '|', @EXCLUDE;

@FILTERED = grep {!/$pattern/} @ELEMENT;

foreach $var (@FILTERED) {
  print "$var\n";
}


NevemTeve 07-18-2018 03:02 AM

I think there is a pre-written program for that, named egrep:

Code:

program_that_produces_many_lines | egrep -v -- '(successfully|execution)'

bigearsbilly 07-18-2018 04:11 AM

how I do it:

Code:

while (<>) {

    next unless m/./;    # ignore blank lines
    next if m/blah/;      # ignore
    next if m/trump/;    # this too
    s/this/that/;        # change 'this' to 'that'
    print;

}
it's easy to read and easy to maintain and will do for most stuff

Code:

command | script.pl > out.log


All times are GMT -5. The time now is 01:07 AM.