LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2008, 03:31 PM   #1
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Rep: Reputation: 30
Is there a function similar to awk for C++?


I have a program that need to scan a string using awk, but it's not script; it's a C++ application. Is there a function that can do that?
 
Old 04-15-2008, 04:23 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
awk is a language, not a function... so what function of awk are you trying to mimic? There are many ways to manipulate strings in C++. You can start by looking at the string class.
 
Old 04-15-2008, 04:36 PM   #3
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Original Poster
Rep: Reputation: 30
I'm trying to separate fields using the ":" as the separator. Such as,
Code:
awk -F":" ' { print $1 }' /etc/group
It just seems like a waste to have to recreate this functionality if it already exists.
 
Old 04-15-2008, 04:36 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You can use regular expressions with <regex.h>, but I don't know about the rest.
ta0kira
 
Old 04-15-2008, 04:36 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by binarybob0001 View Post
I have a program that need to scan a string using awk, but it's not script; it's a C++ application. Is there a function that can do that?
If you're using Qt ... they have a very feature-rich
regex handler for strings.
 
Old 04-15-2008, 04:45 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Maybe you should just use popen with that exact command line?
ta0kira

Edit: I just saw what you're doing. Take a look at strsep.
 
Old 04-15-2008, 07:21 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
If you want to use C-style code, there you can tokenize a string with strtok(). If you want a C++-style analogue, you might have to use something from Boost (e.g., Boost::Tokenizer).
 
Old 04-15-2008, 08:25 PM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Here is a piece of code I posted to another site some time ago.
Code:
std::vector<std::string> split(std::string const& str, std::string const& delim)
{
   using std::string;
   string::size_type start(0);
   string::size_type end(string::npos);
   std::vector<string> sub;

   while( (end = str.find(delim,start)) != string::npos)
   {
      sub.push_back(str.substr(start, end - start));
      start = end + delim.size();
   }
   if(start < str.size() )sub.push_back(str.substr(start, str.size() - start));
   return sub;
}
edit:
Just for good measure I will post the same warning I did when it was originally posted: "This makes some assumptions such as the delimiter does not start the string." It should give you a good idea what needs to be done.

Last edited by dmail; 04-15-2008 at 08:31 PM.
 
Old 04-19-2008, 07:54 PM   #9
binarybob0001
Member
 
Registered: Dec 2004
Distribution: Debian Wheezy
Posts: 444

Original Poster
Rep: Reputation: 30
Thanks for all of your help. At least, I'm not the only one writing the same thing. Perhaps one day when I'm bored I'll write a library that does similar things as our favorite language awk.
 
Old 06-13-2011, 12:04 AM   #10
lvv
LQ Newbie
 
Registered: Jun 2011
Posts: 3

Rep: Reputation: Disabled
There is SCC - awk like C++ snippet compiler. Your:

Code:
awk -F: '{print $1}' /etc/group
will be:

Code:
scc -nF: '$1' /etc/group

Last edited by lvv; 01-11-2012 at 07:47 AM.
 
Old 06-13-2011, 01:49 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
In Linux, there is libpcre; perl-compatible regular expressions. For details:
Code:
man pcre
I don't know whether it is standard fare on most distros. You may have to install it.

--- rod.
 
Old 06-14-2011, 06:41 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,693
Blog Entries: 4

Rep: Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947Reputation: 3947
You should find that libpcre is there.

What you are really looking for is a C++ library class that uses it, so that "all of the arcane magick" is done for you.

... assuming that you really do have to write a C/C++ level program to do this, versus using the very handy and well-debugged C/C++ program (awk itself) that already exists... mmmmm??
 
Old 06-16-2011, 04:43 PM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
a simple example of pcre:

Code:
[0]$ echo hello:old:chap | ./pcre :
hello
old
chap
Code:
#include <iostream>
#include <vector>
#include <string>
#include <pcre++.h>

using namespace std;
using namespace pcrepp;

int main(int argc, char ** argv) {


    string pat = (argc==2)? argv[1]:"\\s+";
    vector<string> v;
    string s;
    Pcre p(pat);

    while( getline(cin, s) ) {
        v = p.split(s);

        vector<string>::iterator i;
        for(i=v.begin();i!=v.end();i++) {
            cout << *i << endl;
        }
    }


    return 0;
}
NOTE: I've tested boost regex, regex.h and pcre and
they still get whipped by perl for speed.

Last edited by bigearsbilly; 06-16-2011 at 04:47 PM.
 
  


Reply



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
Function similar to LocalFileTimeToFileTime in Linux! Jayaraghavendran Linux - Software 1 07-13-2007 12:52 PM
Need help with awk function philosophia Programming 1 02-20-2007 05:53 PM
awk: remove similar lines from logfile peos Programming 7 06-19-2006 07:13 AM
How do I cut out a specific piece of a html page (using sed/awk or similar)? bomix Linux - General 2 10-08-2005 04:30 PM
split files using awk (or similar) lgualteri Programming 1 06-13-2005 09:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:49 PM.

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
Open Source Consulting | Domain Registration