Sponsored Content
Top Forums Shell Programming and Scripting Still trying to get a grep -c that works Post 302188118 by era on Tuesday 22nd of April 2008 04:52:04 PM
Old 04-22-2008
When you use strict, all of your code needs to be strict. It's a hassle at first but it's useful when you start getting useful warnings when the code doesn't do what you expect. If your code is not strict, tackle that later. But adopt it in the next script you write. It's good for you.

The last will quit the foreach loop on the first blank or empty line. If you have empty lines in the patterns file, you probably mean "next", not "last".

If you have the same pattern multiple times in the pattern file, you will get multiple prints of the count of matches. I was under the impression that you were getting 1 login 2 login 3 login but now I see that you have 3 login 3 login 3 login, so it's simply reporting the same pattern multiple times. Welp, you can use a hash to prevent that, too.

Code:
sub runit2 {
  my ($file1a, $file2a) = @_;
  my $file1_vala = $file1a->get;
  my $file2_vala = $file2a->get;
  open (FILE1a, "$file1_vala") or die;
  open (FILE2a, "$file2_vala") or die;
  chomp(my @strings = <FILE2a>);
  close FILE2a;
  $text->insert('end', "Device config contains: \n");
  #*******************************
  foreach $pattern (<FILE1a>) {
    next if $pattern =~ /^\s*$/;
    next if $handled{$pattern};
    $handled{$pattern} = 1;
    chomp($pattern);
    my $matches = grep $pattern eq $_, @strings;
    next unless $matches;
    $text->insert('end', "$matches $pattern \n");
  }
  close FILE1a;
}


Last edited by era; 04-22-2008 at 05:55 PM.. Reason: Optimize so $handled goes before grep
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How the for loop works?

Hi, I have a landing area where some files keep on coming after irregular intervals of time. From this landing area, I need to move files to another directory for processing. For this, I am using a for loop to find certain kinds of files in the landing area. Now my question is, suppose I start... (6 Replies)
Discussion started by: neelaksh
6 Replies

2. UNIX for Dummies Questions & Answers

How ls | wc -l works?

ls displays files in tabbed output. Say a directory contains 3 files. ls will list all 3 in one line. So, I expect ls | wc -l to give 1, but it counts the nr of files and gives 3. Can someone explain how this works? (3 Replies)
Discussion started by: krishmaths
3 Replies

3. Programming

how this works?

pls explain me how this works.... DECODE (SUBSTR (field, 1, 1),'''', '''''' || field || '''','''' || field || '''') here field is a column in an oracle table.... (7 Replies)
Discussion started by: vijay_0209
7 Replies

4. UNIX for Dummies Questions & Answers

>./a.pl works, >a.pl - does not

When I try to execute script, I get message: >aa.pl zsh: command not found: aa.pl but >./aa.pl works OK. What to change in environment to force the former way to work? Thank you, Alex Z (4 Replies)
Discussion started by: zzol
4 Replies

5. Shell Programming and Scripting

how it works ? sftp

Hi, I am curious about this script , how it is running ..? #!/bin/sh echo "OK, starting now..." ftp remotehost <<EOF When I run , it is asking OK, starting now... Password:Name (remotehost): SHould I enter only password ? and explain me how it works.. thanks in advance.. (3 Replies)
Discussion started by: hegdeshashi
3 Replies

6. UNIX for Advanced & Expert Users

How this works?

I have a program............ #include<stdio.h> #include<unistd.h> main() { if(fork == 0) { printf("Hi every body:p!!!!!!!!!!"); } } This program works with out any error. here fork is not a system call. It just act as a variable.But how it works without declaring it? What data type it... (19 Replies)
Discussion started by: carolsanjeevi
19 Replies

7. UNIX for Dummies Questions & Answers

Need a visual grep tool that works on windows via sftp

Hi, Could you please suggest a tool that connects like WINSCP/Putty and allows me to search a remote Unix directory for a certain text pattern (grep) ? Regards, Bhanja. (1 Reply)
Discussion started by: bhanja_trinanja
1 Replies

8. Shell Programming and Scripting

Inconsistent `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l`

i have this line of code that looks for the same file if it is currently running and returns the count. `ps -eaf -o args | grep -i sfs_pcard_load_file.ksh | grep -v grep | wc -l` basically it is assigned to a variable ISRUNNING=`ps -eaf -o args | grep -i sfs_pcard_load_file.ksh |... (6 Replies)
Discussion started by: wtolentino
6 Replies

9. Shell Programming and Scripting

Grep works on Linux but fails on Solaris

Hi, On linux i have the below command working fine. grep -o '<name>.*</name>' deploy.tmp | sed 's/\(<name>\|<\/name>\)//g' deploy.tmp But the same is failing on Solaris uname -a SunOS mymac 5.10 Generic_150400-23 sun4v sparc sun4v Can you tell me how can i get it work on Solaris ?... (6 Replies)
Discussion started by: mohtashims
6 Replies

10. Shell Programming and Scripting

Grep -q not works with multiples grep in same line

Hi, I'm trying to make a grep to see if exists occurrences with a sentence like these: grep -qi "message" file0 | grep -i $date | grep -vi "exception" echo $? 1 If I execute without -q modifier I can find occurrences. Someone could help me please? Thanks and sorry for my English! (1 Reply)
Discussion started by: mierdatuti
1 Replies
WILDMAT(3)						     Library Functions Manual							WILDMAT(3)

NAME
wildmat - perform shell-style wildcard matching SYNOPSIS
int wildmat(text, pattern) char *text; char *pattern; DESCRIPTION
Wildmat is part of libinn (3). Wildmat compares the text against the pattern and returns non-zero if the pattern matches the text. The pattern is interpreted according to rules similar to shell filename wildcards, and not as a full regular expression such as those handled by the grep(1) family of programs or the regex(3) or regexp(3) set of routines. The pattern is interpreted as follows: x Turns off the special meaning of x and matches it directly; this is used mostly before a question mark or asterisk, and is not spe- cial inside square brackets. ? Matches any single character. * Matches any sequence of zero or more characters. [x...y] Matches any single character specified by the set x...y. A minus sign may be used to indicate a range of characters. That is, [0-5abc] is a shorthand for [012345abc]. More than one range may appear inside a character set; [0-9a-zA-Z._] matches almost all of the legal characters for a host name. The close bracket, ], may be used if it is the first character in the set. The minus sign, -, may be used if it is either the first or last character in the set. [^x...y] This matches any character not in the set x...y, which is interpreted as described above. For example, [^]-] matches any character other than a close bracket or minus sign. HISTORY
Written by Rich $alz <rsalz@uunet.uu.net> in 1986, and posted to Usenet several times since then, most notably in comp.sources.misc in March, 1991. Lars Mathiesen <thorinn@diku.dk> enhanced the multi-asterisk failure mode in early 1991. Rich and Lars increased the efficiency of star patterns and reposted it to comp.sources.misc in April, 1991. Robert Elz <kre@munnari.oz.au> added minus sign and close bracket handling in June, 1991. This is revision 1.10, dated 1992/04/03. SEE ALSO
grep(1), regex(3), regexp(3). WILDMAT(3)
All times are GMT -4. The time now is 03:36 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy