Sponsored Content
Full Discussion: perl equivalent to grep -c
Top Forums Shell Programming and Scripting perl equivalent to grep -c Post 302186723 by era on Friday 18th of April 2008 02:02:13 AM
Old 04-18-2008
You don't really need the array for @file1 then, you can just run another while loop over that and save the memory for the array which only got used during initialization of the hash.

You also don't need to chomp the values, as long as you are consistent on whether or not you chomp.

This assumes you actually want to simulate "grep -x" (match whole lines), not find a token anywhere on a line in file2. If that's what you actually want, maybe something like this would work.

Code:
open (FILE1, "SOURCEFILE.TXT") or die "$0: Could not open SOURCEFILE.TXT: $!\n";
open (FILE2, "RESULTFILE.TXT") or die "$0: Could not open RESULTFILE.TXT: $!\n";

@RESULTARRAY = <FILE2>;
foreach $lineinthesourcefile (<FILE1>) {
  chomp $lineinthesourcefile;  # trim trailing newline
  @matches = grep { /$lineinthesourcefile/ } @RESULTARRAY;
  print $lineinthesourcefile, " ", scalar @matches, "\n" if @matches;
}

The "grep" finds the elements in @RESULTARRAY matching the condition in { } and returns those as a list. If that list has any elements, we print a report. (scalar @matches returns how many elements @matches has.)

This matches regardless of context, so "car" will be matched inside another word, like "scars" or "career". You can dress up the regular expression in the grep some if you want different behavior.

I was tempted to use @INSANELYLONGUPPERCASEVARIABLENAME instead of @matches, but if that's what you want, that's obviously easy to fix. <ducks>
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

export equivalent command in PERL

Hi I need an equivalent command in PERL for the following. export LC_ALL=C; I hope this is the command. Please confirm this and correct me if i am wrong $ENV{LC_ALL}="C"; Thanks and Regards Ammu (1 Reply)
Discussion started by: ammu
1 Replies

2. Shell Programming and Scripting

strtok equivalent in perl

Hi All, Is their any equivalent for strtok (in c) to use in perl script. Thanks in advance. JS (1 Reply)
Discussion started by: jisha
1 Replies

3. Shell Programming and Scripting

need perl equivalent

Dear All, Good day, can any of you help me in the following problem: I need to find the perl equivalent for the following commandline grep characters |awk '{print \$2}'Expecting your reply and thanks in advance. Warm regards Fredrick. (4 Replies)
Discussion started by: Fredrick
4 Replies

4. Shell Programming and Scripting

grep -v equivalent in perl

I have to do grep -v in a perl script. I want to exclude blank lines and lines having visitor. #grep -v visitor abc.txt |grep '.' file:abc.txt 1340 not booked 16D:D9 tourist 8 1341 not booked 16C:D4 tourist 25 1342 not booked 16D:C4 visitor 7 1343 not booked 01C:D9 visitor 6 1344... (4 Replies)
Discussion started by: dynamax
4 Replies

5. Shell Programming and Scripting

GAWK/GREP Equivalent

What is GAWK equivalent to greps -B 5 -A 5? zgrep -i "^oct 20" /var/log/syslog*|grep -iB 5 -A 5 'postfix\/pickup /var/log/syslog.1.gz:Oct 20 01:55:01 elmo CROND: (mail) CMD (/usr/bin/python -S /usr/lib64/mailman/cron/gate_news) /var/log/syslog.1.gz:Oct 20 02:00:01 elmo CROND: (mail) CMD... (5 Replies)
Discussion started by: metallica1973
5 Replies

6. Shell Programming and Scripting

What is the equivalent of NR (awk) in perl?

Hello, I searched online; it seems that perl use $NR as NR in awk; however it does not work for me. For example, how to re-write the following awk using perl: awk '{ print NR}' inputfile---------- Post updated at 01:55 PM ---------- Previous update was at 12:49 PM ---------- I found... (2 Replies)
Discussion started by: littlewenwen
2 Replies

7. Shell Programming and Scripting

Perl equivalent substitution

hi Geeks, my input file contains data like => 53 - Deewana Kar Raha Hai.mp3 54 - Hale Dil.mp3 55 - Ishq Sufiyana.mp3 56 - Abhi Kuch Dino Se.mp3 57 - Pee Loon Hoto Ki Sargam.mp3 I had used sed command to remove the prefix from the file name like sed 's/^\ it gives me the perfect... (4 Replies)
Discussion started by: lohith.dutta
4 Replies

8. Shell Programming and Scripting

sed Equivalent for awk/grep

Any equivalent command using awk or grep? sed -n "/^$(date --date='10 minutes ago' '+%b %_d %H:%M')/,\$p" /abc.log (7 Replies)
Discussion started by: timmywong
7 Replies

9. Shell Programming and Scripting

Perl : Perl equivalent to the ksh | and ;

Ive been trying to move to Perl. It has been a struggle. My question is, is there a good resource that explains nesting statements. As an example. To change primary Factory CTS 1.9.0(46) P1 *Slot 1 CTS 1.10.2(42) P1 To primary *Slot 1 CTS 1.10.2(42) P1 ... (5 Replies)
Discussion started by: popeye
5 Replies

10. AIX

Need grep -v Equivalent for AIX

Need grep -v "Hello" equivalent for AIX (9 Replies)
Discussion started by: mohtashims
9 Replies
Net::DNS::RR::TXT(3)					User Contributed Perl Documentation				      Net::DNS::RR::TXT(3)

NAME
Net::DNS::RR::TXT - DNS TXT resource record SYNOPSIS
use Net::DNS; $rr = new Net::DNS::RR( 'name TXT txtdata ...' ); $rr = new Net::DNS::RR( name => 'name', type => 'TXT', txtdata => 'single text string' ); $rr = new Net::DNS::RR( name => 'name', type => 'TXT', txtdata => [ 'multiple', 'strings', ... ] ); use utf8; $rr = new Net::DNS::RR( 'jp TXT XXXXXXXXXXXX' ); DESCRIPTION
Class for DNS Text (TXT) resource records. METHODS
The available methods are those inherited from the base class augmented by the type-specific methods defined in this package. Use of undocumented package features or direct access to internal data structures is discouraged and could result in program termination or other unpredictable behaviour. txtdata $string = $rr->txtdata; @list = $rr->txtdata; $rr->txtdata( @list ); When invoked in scalar context, txtdata() returns the descriptive text as a single string, regardless of the number of elements. In a list context, txtdata() returns a list of the text elements. COPYRIGHT
Copyright (c)2011 Dick Franks. All rights reserved. This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself. Package template (c)2009,2012 O.M.Kolkman and R.W.Franks. SEE ALSO
perl, Net::DNS, Net::DNS::RR, RFC1035 Section 3.3.14, RFC3629 perl v5.18.2 2014-01-16 Net::DNS::RR::TXT(3)
All times are GMT -4. The time now is 02:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy