Basic guide to terminal piping

I'm a total beginner at this, but I'm thinking it will be very useful to me. I'm not sure if this is supposed to be in the UNIX/Linux section or not, but I found that an answer to this is likely to attract and help beginner programmers who are programing on Linux.

Is anyone willing to guide me through piping and directing output in the Linux terminal? Just write a short article on the matter explaining a few of the most used functions

Is it possible to pipe information into function of another program?

This is my compiling command atm:

g++ file.cpp -o program -Wall -Wextra -Werror 2> Compile.txt

I get output like this ( Don't worry about the errors they are purposely made to test this )


ostreamoverloading.cpp:27:1: error: ‘friend’ used outside of class
 friend std::istream &operator>> (std::istream& is, Obj& o)
 ^
ostreamoverloading.cpp: In function ‘std::istream& operator>>(std::istream&, Obj&)’:
ostreamoverloading.cpp:9:17: error: ‘std::string Obj::output’ is private
     std::string output;
                 ^
ostreamoverloading.cpp:30:24: error: within this context
     std::getline(is, o.output;
                        ^
ostreamoverloading.cpp:30:30: error: expected ‘)’ before ‘;’ token
     std::getline(is, o.output;


I'm wondering if I can get the output more like:

Compile.txt

ostreamoverloading.cpp:27:1: error: ‘friend’ used outside of class
ostreamoverloading.cpp:9:17: error: ‘std::string Obj::output’ is private
ostreamoverloading.cpp:30:30: error: expected ‘)’ before ‘;’ token



If I can't do something like that, perhaps another way is redirecting it to another program which will parse it?

Something like:

g++ file.cpp -o program -Wall -Wextra -Werror > ./ErrorParser > Parsed.txt


I'm looking forward to responses!
I've literally pulled this out of my ass, never used a vector before in my life but used what I've seen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <vector>

int main(int argc, char* argv[])
{
	if(argc > 1)
	{
		std::ifstream ifs(argv[1]);
		std::vector<std::string> lines;
		while(ifs)
		{
			std::string line;
			std::getline(ifs, line);
			lines.push_back(line);
		}
		for(int i = 0; i < lines.size(); i++)
		{
			std::string temp;
			temp = lines[i];
			if(temp.find("error:") != std::string::npos) std::cout << lines[i] << "\n";
		}
	}
	else
	{
		std::cerr << "Incorrect usage\n";
		return 1;
	}
	return 0;
}


My only problem is that it catches the useless:

ostreamoverloading.cpp:30:24: error: within this context


EDIT: Fixed using this

1
2
3
4
5
6
7
8
for(int i = 0; i < lines.size(); i++)
{
	std::string temp;
	temp = lines[i];
	if(temp.find("error:") != std::string::npos)
	if(temp.find("within this context") != std::string::npos) continue;
	else std::cout << lines[i] << "\n";
}


EDIT 2:

Improved code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]) // To use the program type program file.txt
{
	if(argc > 1) // If they are using it correctly
	{
		std::ifstream ifs(argv[1]); // Open the file they specified
		while(ifs) // while they file is open
		{
			std::string line;
			std::getline(ifs, line); // Get each line

			if(line.find("error:") != std::string::npos)
				if(line.find("within this context") != std::string::npos) continue;
				else std::cout << line << "\n";
		}
                return 0;
	}
	else std::cerr << "Incorrect usage\n";
	return 1;
}
Last edited on
g++ ... | grep "error:" | grep -v "within this" | tee Parsed.txt

@keskiverto

That's a great help, thanks.

Is there a way of piping input from one program to another?
What do you mean? The pipe foo | bar directs the standard output of foo into the standard input of bar.
Say I have a program the generates some output, then another program that takes that output as input.

./GenerateOutput | ./ParseOutput | ./TakeParsedOutputAsInput
What do you think to happen in the:
g++ ... | grep "error:" | grep -v "within this" | tee Parsed.txt

The standard output of g++ is piped to grep.
The standard output of grep is piped to grep.
The standard output of grep is piped to tee.

The standard output of g++ is piped to grep.
The standard output of grep is piped to grep.
The standard output of grep is piped to tee.


Yes, I understand that. My question is more how do you get your own program to take that kind of input?

I've been looking over JLBorges code:

1
2
std::string line ;
while( std::getline( std::cin, line ) ) std::cout << std::string( line.rbegin(), line.rend() ) << '\n' ;


I can see the theory behind it, I'm just not exactly sure how the output from another program is sent directly into std::cin.
Topic archived. No new replies allowed.