Sponsored Content
Top Forums Programming Wildcard Pattern Matching In C Post 302976672 by Azrael on Sunday 3rd of July 2016 01:14:22 PM
Old 07-03-2016
Sorry, I thought I had provided the contents of the file earlier. Here it is again with just the following for testing:

Code:
$ cat /var/log/apache2/other_vhosts_access.log
03/Jul blah pattern

I did not know regex in C and grep/egrep were so closely related. I did as Jim suggested and saw nothing was matched:

Code:
$ egrep "03?Jul*pattern" /var/log/apache2/other_vhosts_access.log | wc -l
0

I changed this to the following and it was matched:

Code:
egrep "03.Jul.*pattern" /var/log/apache2/other_vhosts_access.log | wc -l
1

Still not getting a match when I try applying this to C though:
Code:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <regex.h>

int main() {

   char newhold[40];

        regex_t re;
        time_t current = time(NULL);
        char day[10];
        char mon[10];
        int retval = 0;

        strftime(day, sizeof(day), "%d", localtime(&current));
        strncat( newhold, day, 10 );
        strncat( newhold, ".", 2 );
        strftime(mon, sizeof(mon), "%b", localtime(&current));
        strncat( newhold, mon, 10 );
        strncat( newhold, ".*", 4 );
        strncat( newhold, "pattern", 10 );

     if(regcomp(&re , newhold, REG_NOMATCH) != 0 ){
         return 1;
     }

    FILE *fp;
    fp = fopen("/var/log/apache2/other_vhosts_access.log", "r");
    char line[100];
    unsigned int i = 0;

    while(fgets(line, sizeof(line), fp)) {
        if ((retval = regexec(&re, line, 0, NULL, 0)) == 0){
        i++;
      }
    }
  printf("%s %d\n", newhold, i );
  fclose(fp);
  return 0;
}

I also tried working with regerror(), but had trouble applying the few examples I was able to find.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Find wildcard .shtml files in wildcard directories and removing them- How's it done?

I'm trying to figure out how to build a small shell script that will find old .shtml files in every /tgp/ directory on the server and delete them if they are older than 10 days... The structure of the paths are like this: /home/domains/www.domain2.com/tgp/ /home/domains/www.domain3.com/tgp/... (1 Reply)
Discussion started by: Neko
1 Replies

2. Shell Programming and Scripting

comment/delete a particular pattern starting from second line of the matching pattern

Hi, I have file 1.txt with following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433 ** ** ** In file 2.txt I have the following entries as shown: 0152364|134444|10.20.30.40|015236433 0233654|122555|10.20.30.50|023365433... (4 Replies)
Discussion started by: imas
4 Replies

3. Shell Programming and Scripting

counting the lines matching a pattern, in between two pattern, and generate a tab

Hi all, I'm looking for some help. I have a file (very long) that is organized like below: >Cluster 0 0 283nt, >01_FRYJ6ZM12HMXZS... at +/99% 1 279nt, >01_FRYJ6ZM12HN12A... at +/99% 2 281nt, >01_FRYJ6ZM12HM4TS... at +/99% 3 283nt, >01_FRYJ6ZM12HM946... at +/99% 4 279nt,... (4 Replies)
Discussion started by: d.chauliac
4 Replies

4. UNIX for Dummies Questions & Answers

sed non-greedy pattern matching with wildcard

Toby> cat sample1 This is some arbitrary text before var1, This IS SOME DIFFERENT ARBITRARY TEXT before var2 Toby> sed -e 's/^This .* before //' -e 's/This .* before //' sample1 var2 I need to convert the above text in sample1 so that the output becomes var1, var2 by... (2 Replies)
Discussion started by: TobyNorris
2 Replies

5. UNIX for Dummies Questions & Answers

Find pattern suffix matching pattern

Hi, I am trying to get a result out of this but fails please help. Have two files /tmp/1 & /tmp/hosts. /tmp/1 IP=123.456.789.01 WAS_HOSTNAME=abcdefgh.was.tb.dsdc /tmp/hosts 123.456.789.01 I want this result in /tmp/hosts if hostname is already there dont want duplicate entry. ... (5 Replies)
Discussion started by: rajeshwebspere
5 Replies

6. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

7. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

8. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

9. Shell Programming and Scripting

Grep and BzGrep with Wildcard in Search Pattern

Hello All, I hope this is the right area. If not, Kindly let me know and I will report in the appropriate spot. I am needing to find a search pattern that will make the * act as Wildcard in the search pattern instead of being literal. The example I am using is bzgrep "to=<*@domain.com>"... (5 Replies)
Discussion started by: mancountry
5 Replies

10. Shell Programming and Scripting

Replace String matching wildcard pattern

Hi, I know how to replace a string with another in a file. But, i wish to replace the below string pattern EncryptedPassword="{gafgfa}]\asffafsf312a" i.e EncryptedPassword="<any random string>" To EncryptedPassword="" i.e remove the random password to a empty string. Can you... (3 Replies)
Discussion started by: mohtashims
3 Replies
gmatch(3GEN)                                         String Pattern-Matching Library Functions                                        gmatch(3GEN)

NAME
gmatch - shell global pattern matching SYNOPSIS
cc [ flag ... ] file ... -lgen [ library ... ] #include <libgen.h> int gmatch(const char *str, const char *pattern); DESCRIPTION
gmatch() checks whether the null-terminated string str matches the null-terminated pattern string pattern. See the sh(1), section File Name Generation, for a discussion of pattern matching. A backslash () is used as an escape character in pattern strings. RETURN VALUES
gmatch() returns non-zero if the pattern matches the string, zero if the pattern does not. EXAMPLES
Example 1: Examples of gmatch() function. In the following example, gmatch() returns non-zero (true) for all strings with "a" or "-" as their last character. char *s; gmatch (s, "*[a-]" ) ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sh(1), attributes(5) NOTES
When compiling multithreaded applications, the _REENTRANT flag must be defined on the compile line. This flag should only be used in mul- tithreaded applications. SunOS 5.10 29 Dec 1996 gmatch(3GEN)
All times are GMT -4. The time now is 11:49 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy