LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2018, 12:32 PM   #1
bishop2001
Member
 
Registered: Apr 2015
Posts: 50

Rep: Reputation: Disabled
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
 
Old 07-17-2018, 12:55 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 20,665

Rep: Reputation: 5079Reputation: 5079Reputation: 5079Reputation: 5079Reputation: 5079Reputation: 5079Reputation: 5079Reputation: 5079Reputation: 5079Reputation: 5079Reputation: 5079
Quote:
Originally Posted by bishop2001 View Post
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

Last edited by TB0ne; 07-17-2018 at 12:59 PM.
 
Old 07-17-2018, 03:26 PM   #3
bishop2001
Member
 
Registered: Apr 2015
Posts: 50

Original Poster
Rep: Reputation: Disabled
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
 
Old 07-17-2018, 03:49 PM   #4
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,813

Rep: Reputation: 570Reputation: 570Reputation: 570Reputation: 570Reputation: 570Reputation: 570
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?

Last edited by scasey; 07-17-2018 at 04:05 PM.
 
Old 07-17-2018, 04:02 PM   #5
bishop2001
Member
 
Registered: Apr 2015
Posts: 50

Original Poster
Rep: Reputation: Disabled
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;
}
}
 
Old 07-17-2018, 04:13 PM   #6
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 1,813

Rep: Reputation: 570Reputation: 570Reputation: 570Reputation: 570Reputation: 570Reputation: 570
Quote:
Originally Posted by bishop2001 View Post
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.
 
1 members found this post helpful.
Old 07-17-2018, 06:28 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Quote:
Originally Posted by bishop2001 View Post
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";
}
 
Old 07-18-2018, 03:02 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,597

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

Code:
program_that_produces_many_lines | egrep -v -- '(successfully|execution)'
 
Old 07-18-2018, 04:11 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,432

Rep: Reputation: 203Reputation: 203Reputation: 203
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
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep in loop with exact match udiubu Linux - Newbie 4 12-02-2017 07:31 AM
[SOLVED] stupid simple for loop to match usernames in /etc/features linux_evangelist Linux - Newbie 4 11-12-2011 02:11 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
Bash: proceeding only if a string from a for loop does *not* match some value kylebarbour Programming 11 03-12-2009 04:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration