LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Is there a C equivalent to grep? (https://www.linuxquestions.org/questions/linux-software-2/is-there-a-c-equivalent-to-grep-4175657280/)

RandomTroll 07-12-2019 05:18 PM

Is there a C equivalent to grep?
 
I wrote a program. The first iteration I passed every parameter on the command line. That got unwieldy as I had to add more. I'd like to have it read a configuration file instead. If it were a script, I'd just have it
Code:

BaseName=`grep -w ^BaseName ConfigurationFile | cut -d: -f2`
Is there anything more convenient than strstr?

rtmistler 07-12-2019 06:50 PM

Can't really tell what you're trying to do.

strstr() is fairly convenient if you know what you need it for and how to use it.

As far as other library functions, the grep page for the first one usually has a SEE ALSO section.

As far as programming convenience, once can always code string searches regardless of library use.

One can also look at the source code for each of grep and also strstr().

individual 07-12-2019 08:14 PM

There isn't a convenient function included with the standard library, but using existing functions you can roll your own. If you want I'll share a function that I use when I need simple pattern matching.

RandomTroll 07-13-2019 06:50 AM

Quote:

Originally Posted by rtmistler (Post 6014502)
Can't really tell what you're trying to do.

I want a program to read parameters from a configuration file instead of the command line. For instance I want to have a plain-text initialization file that looks like:

Quote:

Fruit: Apple
Fish: Trout
Vegetable: Asparagus
and have the program assign the value Apple to the variable Fruit...

Quote:

Originally Posted by rtmistler (Post 6014502)
One can also look at the source code for each of grep and also strstr().

Duh. I can also write it in assembly and use no libraries.

Quote:

Originally Posted by individual (Post 6014511)
There isn't a convenient function included with the standard library

It's done so often that surprises me. I found getconf and confstr, but they don't seem to apply

Quote:

Originally Posted by individual (Post 6014511)
If you want I'll share a function that I use when I need simple pattern matching.

That'd be nice. I started by fopen-ing it, reading it line-ly with getline, picking out the first word, running that through switch on all the possible variables.

Later: discovered strtok, the function perfect for separating elements on a line.

individual 07-13-2019 10:05 AM

EDIT: Solved, I guess?

RandomTroll 07-13-2019 04:19 PM

Yes. It would have been nice to have a switch that worked on strings, but a cascade of else ifs isn't awful. If I were really serious I'd convert to a database and query that.

RandomTroll 07-13-2019 09:17 PM

Useful note: fgets, getline, etc., emit the newline with the last token on the line. That took a while to figure out and strip out.

WideOpenSkies 07-13-2019 09:56 PM

Quote:

Originally Posted by RandomTroll (Post 6014812)
Useful note: fgets, getline, etc., emit the newline with the last token on the line. That took a while to figure out and strip out.

getline is only on C++, no? If so, that might not count because OP is specifically asking about C.

individual 07-13-2019 10:08 PM

Quote:

Originally Posted by Contrapak (Post 6014821)
getline is only on C++, no? If so, that might not count because OP is specifically asking about C.

RandomTroll is the OP. :)

There is a C++ version of getline, but getline is part of POSIX. From the man page:
Quote:

Both getline() and getdelim() were originally GNU extensions. They were standardized in POSIX.1-2008.

Mechanikx 07-13-2019 11:11 PM

Quote:

Originally Posted by RandomTroll (Post 6014812)
Useful note: fgets, getline, etc., emit the newline with the last token on the line. That took a while to figure out and strip out.

In one of my C books I'm studying the author provides his own function for testing for the newline and removing it if it's been read by fgets().

I'll post his solution in case you might find it useful.

Code:

char *s_gets(char *st, int n)
{
    char *ret_val;
    char *find;

    ret_val = fgets(st, n, stdin);
    if (ret_val)
    {
        find = strchr(st, '\n');    /* look for newline */
        if (find)                  /* if the address is not null... */
            *find = '\0';          /* ...place a null character there */
        else
            while (getchar() != '\n')    /* dispose of rest of line */
                continue;
    }
    return ret_val;
}


ehartman 07-13-2019 11:13 PM

Quote:

Originally Posted by individual (Post 6014511)
There isn't a convenient function included with the standard library, but using existing functions you can roll your own. If you want I'll share a function that I use when I need simple pattern matching.

To get more grep-like behaviour, you can use the functions contained in the pcre(3) library
Quote:

The PCRE library is a set of functions that implement regular expression pattern matching using the same syntax and semantics as Perl, with just a few differences.
Some features that appeared in Python and PCRE before they appeared in Perl are also available using the Python syntax, there is some support for one or two .NET and Oniguruma syntax items, and there is an option for requesting some minor changes that give better JavaScript compatibility.
This probably goes much further then what the OP requested, but it is a possible answer to the subject.

RandomTroll 07-14-2019 12:35 PM

I've already written the program in C. I used strcspn to reject newlines.


All times are GMT -5. The time now is 02:00 PM.