LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 05-28-2008, 12:39 PM   #1
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Rep: Reputation: 15
Question matching array's lines??


I am attempting to match lines in a huge log files, reading each line into an array then righting it to an email. That part is easy, now they want me to print only certain lines out. I have a 2nd array thats sole job is to be used to match a field. I can do it in with three individual IF statements.

Is there an easier way to do it, with the =~ like $a[1] =~ %b ???? or something that? To compare the field to the entire 2nd array at once?
 
Old 05-28-2008, 02:40 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
What language is this?
 
Old 05-28-2008, 02:48 PM   #3
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
Question

I am sorry it is in perl

I am looking for a way to compare 1 element of an array $a[1] to another entire array @b

I also could do it with a for loop, isn't there a one liner to do it?
 
Old 05-28-2008, 03:38 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
You mean a boolean OR of each element of the second array as the match string? Why not just do that (i.e., just collapse your array into a single string separated by ‘|’)?

To clarify, instead of something like this:
Code:
if ($a[1] =~ /$b[0]/ || $a[1] =~ /$b[1]/ || … || $a[1] =~ /$b[n]) {
    foo
}
You would have this:
Code:
my $match = join '|', @b;

if ($a[1] =~ /$match/) {
    foo
}
 
Old 05-28-2008, 06:09 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Sounds to me you want the Perl grep: http://perldoc.perl.org/functions/grep.html
 
Old 05-28-2008, 06:56 PM   #6
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by chrism01 View Post
Sounds to me you want the Perl grep
I see how you would want grep for the overall email filtering, but I don’t understand how you would use grep for the ORing of each pattern match.
 
Old 05-28-2008, 11:49 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, the orig qn has varied a bit.
I was orig thinking of an answer to post #3 ie 1 element compared to an array.
However, if OP wants to compare a list/array of elements against the input file/array, then combine your approach ie create an ORed regex and use my grep to match against the whole file/array.
Note that this may not be a good idea if the post 1 ('huge file') is accurate.
You'd be doing against 1 rec at a time if its too big to (easily) fit in mem, although I suppose you could do it in blocks of eg 100 lines at a time.
 
Old 05-29-2008, 07:42 AM   #8
HyperTrey
Member
 
Registered: Sep 2006
Posts: 127

Original Poster
Rep: Reputation: 15
ended up using the for loop. Easiest way to perform all the other tasks that are supposed to be done to the large log file correctly.
 
  


Reply



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
sed or grep : delete lines containing matching text raj000 Linux - General 18 09-08-2012 09:38 AM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 05:08 PM
awk/gawk/sed - read lines from file1, comment out or delete matching lines in file2 rascal84 Linux - General 1 05-24-2006 09:19 AM
regular expresions: java: matching lines with out string zymos Programming 1 05-28-2005 09:17 AM
error printing array's phek Programming 2 06-05-2002 06:59 AM

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

All times are GMT -5. The time now is 05:06 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
Open Source Consulting | Domain Registration