LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-12-2019, 05:18 PM   #1
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,248

Rep: Reputation: 202Reputation: 202Reputation: 202
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?
 
Old 07-12-2019, 06:50 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,778
Blog Entries: 13

Rep: Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272Reputation: 3272
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().
 
Old 07-12-2019, 08:14 PM   #3
individual
Member
 
Registered: Jul 2018
Posts: 207

Rep: Reputation: 155Reputation: 155
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.
 
Old 07-13-2019, 06:50 AM   #4
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,248

Original Poster
Rep: Reputation: 202Reputation: 202Reputation: 202
Quote:
Originally Posted by rtmistler View Post
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 View Post
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 View Post
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 View Post
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.

Last edited by RandomTroll; 07-13-2019 at 10:05 AM. Reason: Add information
 
Old 07-13-2019, 10:05 AM   #5
individual
Member
 
Registered: Jul 2018
Posts: 207

Rep: Reputation: 155Reputation: 155
EDIT: Solved, I guess?

Last edited by individual; 07-13-2019 at 10:17 AM.
 
Old 07-13-2019, 04:19 PM   #6
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,248

Original Poster
Rep: Reputation: 202Reputation: 202Reputation: 202
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.
 
Old 07-13-2019, 09:17 PM   #7
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,248

Original Poster
Rep: Reputation: 202Reputation: 202Reputation: 202
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.
 
Old 07-13-2019, 09:56 PM   #8
Contrapak
Member
 
Registered: May 2019
Location: /home/
Distribution: Arch Linux
Posts: 116

Rep: Reputation: 16
Quote:
Originally Posted by RandomTroll View Post
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.
 
Old 07-13-2019, 10:08 PM   #9
individual
Member
 
Registered: Jul 2018
Posts: 207

Rep: Reputation: 155Reputation: 155
Quote:
Originally Posted by Contrapak View Post
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.
 
Old 07-13-2019, 11:11 PM   #10
Mechanikx
Member
 
Registered: Jul 2018
Location: Canada
Distribution: Slackware
Posts: 149

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by RandomTroll View Post
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;
}
 
Old 07-13-2019, 11:13 PM   #11
ehartman
Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 672

Rep: Reputation: 317Reputation: 317Reputation: 317Reputation: 317
Quote:
Originally Posted by individual View Post
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.
 
1 members found this post helpful.
Old 07-14-2019, 12:35 PM   #12
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,248

Original Poster
Rep: Reputation: 202Reputation: 202Reputation: 202
I've already written the program in C. I used strcspn to reject newlines.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] awk equivalent to grep, sed, sort pipe danielbmartin Programming 4 06-26-2013 04:59 AM
[SOLVED] GAWK/GREP Equivalent metallica1973 Programming 2 10-23-2012 01:54 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
equivalent to grep inside nawk script block ade05fr Programming 1 04-11-2012 03:58 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:30 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration