Click to See Complete Forum and Search --> : Pipe in unix


tux0r
October 23rd, 2002, 09:13 AM
Hi,

I did a search on this forum and on google but I'm not able to find what I'm looking for!

When you're on a unix shell and want to look the number of occurance of the word "all" that are in a text, you'll do (one way to do it) :

# cat myfile | grep all | wc -w

* maybe the syntax is wrong but that's not the point :) *

Now let's assume I'm programming 'wc' in C or C++ (both are ok).
What I've found on google is 'popen' but it needs "cat myfile | grep all" as argument nb 1.

How do I do to get this from the shell ?
Maybe I'm thinking of it the wrong way.

Any help would be appreciated!

Regards

-tux

Philip Nicoletti
October 23rd, 2002, 11:05 AM
This worked under Linux and Irix for me using g++.
Note: I got it from the popen man page on my Irix
system. I don't know where BUFSIZ comes from -
must be one of the headers.


#include <cstdio>
#include <cstdlib>

int main()
{
char *cmd = "cat m.cpp | grep int | wc -w";
char buf[BUFSIZ];
FILE *ptr;

if ((ptr = popen(cmd, "r")) != NULL)
{
while (fgets(buf, BUFSIZ, ptr) != NULL)
(void) printf("%s", buf);
}
return 0;
}

tux0r
October 23rd, 2002, 12:11 PM
Philip thanx for your answer.

The point is I specified "Now let's assume I'm programming 'wc' ".

I don't want to get the output of "cat m.cpp | grep int | wc -w"

How do wc get the output from "cat m.cpp | grep int" ?

Regards

-tux

Philip Nicoletti
October 23rd, 2002, 12:27 PM
I'm not sure I quite understand. Doesn't just using :


char *cmd = "cat m.cpp | grep int";


in my sample code give you that output ?

tux0r
October 23rd, 2002, 12:46 PM
Excuse my english... Maybe I'm not clear.
It's not my motherthongue.

I am programming 'wc'.

wc is a program that counts the numbers of words, line or caracters in a file.
it can also be used through a pipe.

Anyway here is the thing.. in my 'wc' program you advise me to put :

char *cmd = "cat m.cpp | grep int";

ok I can do it easily but what if the guys want to do "cat another.file | wc -c"

file name changed and no more grep.

Do you understand what I mean ?

Yves M
October 23rd, 2002, 01:23 PM
As far as I understand it, the | piping works with standard input and standard output. So all you have to do is write a program that uses either scanf (for C) or cin (for C++) and then write the results using printf or cout.

I once wrote a simple text database tool under Linux that used piping for combining operations.

Like
column Customer Address | selectrows "Customer=Yves *" | column Address

to get the addresses of customers with first name Yves. All I did was using scanf and printf.

Elusive
October 24th, 2002, 12:25 AM
As Yves said, just use your standard input & output handles.
Dont worry about using popen(), the shell will have the pipes (if any) setup for you,
so just read from your stdin unless files have been specified on the command line.

tux0r
October 24th, 2002, 06:02 AM
OK Thanx guys.

For the moment I have no idea how to write the lines but I have to think about it.. I don't have much time at the moment (lot of stuff at work).

Regards

-tux