to get the specific results at the different time steps.

so i need a bit of guidance. i have a program that outputs results in several time steps in a text file. what i want to do is get the specific results at the different time steps.

Example.
What I want to do is just to read and print out onto a text file the values of C of numbers 2, 4, 5 for each time step. Thanks

A B C
1 217.51 -0.000019674 -1.5148E-18
2 187 -4.6412E-07 -7.3596E-18
3 207 -2.6682E-06 -2.5078E-18
4 260 -1.0736E-11 -1.3375E-18
5 280 2.6058E-10 3.3835E-10
-----------------------------------------------
1 280 4.0676E-22 9.585E-22
2 260 -2.3479E-15 -3.8533E-18
3 280 1.5539E-14 2.5181E-14
4 260 4.35E-22 3.0534E-21
5 280 1.4176E-19 3.1043E-19
---------------------------------------------
1 260 -2.6471E-20 -7.5713E-21
2 280 -1.2346E-19 -3.972E-20
3 260 1.201E-26 3.9328E-26
4 280 -3.4254E-25 -1.2022E-25
5 260 -4.3254E-25 -2.4788E-25
C++ is the wrong tool for this. Just use the shell.
Something like
% grep -E '^[245]\b' input_file | awk '{ print $4 }' > output_file
Thanks Mbozzi,
I use the shell independently which works fine but the issue is the results is linked to a C++ algorithm to run further analysis. So everything is in one C++ source code hence can not run only this part independently on a shell.
Unfortunate. You can't just call std::system?

Maybe something like:
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 <string>
# include <sstream>

struct record { int event; int a; double b, c; };

std::istream& operator>>(std::istream& is, record& r) 
{ return is >> r.event >> r.a >> r.b >> r.c; }

bool parse_record(std::string const& line, record& r) 
{ return (std::istringstream{line} >> r).good(); }

bool accept_record(record const& r) 
{ return r.event == 2 || r.event == 3 || r.event == 5; }

int main() {
    for (std::string line; std::getline(std::cin, line);) {
        record r; 
        if (parse_record(line, r) && accept_record(r)) 
            std::cout << r.c << '\n';
    }
}

Live demo:
http://coliru.stacked-crooked.com/a/ecc33b1a469e320b
Last edited on
well if call std::system does it mean that if i compile my overall code into (.exe) executable form the linux command will be part which will work perfectly?
std::system spawns a shell directly -- so it's dependent on the environment, and not really portable. You might need to modify the command according to the machine's environment in general.

This is the primary advantage of std::system (that it gives control to the environment), but if that isn't desired it isn't a suitable solution.

awk and egrep exist on most Unix systems. But the Windows shell is famously awful in comparison to Unix, and it provides neither of those utilities. (IIRC, there's FINDSTR.EXE, but I don't know any details.)
Last edited on
> awk and egrep exist on most Unix systems. But the Windows shell is famously awful in
> comparison to Unix, and it provides neither of those utilities.

awk, grep etc. are separate programs; and the windows shell does support piping and redirection.

Windows versions (caveat: viral) of these Unix programs: http://gnuwin32.sourceforge.net/packages.html
you can also install cygwin and cut the windows path to see the executables, allowing invocation from the dos shell and your programs.

if the program is to be distributed to other people, you have to install the unix commands alongside your code. If that becomes an aggravation, this is NOT a difficult problem to solve the old fashioned way ... a fairly straightforward parse of the file.
Topic archived. No new replies allowed.