0

I use a simulator (ndnSIM) in Linux which is based on the C++. After doing some tasks, it prints an output in each second in the stdout by this command:

L3RateTracer::InstallAll ("-", Seconds (1));. This is constant and I cannot change its format to print the output in a string or an array. I want to extract a specific data from this output from stdout. For instance, the output in the 1st second has been printed in stdout as follows (the space between each feature is by Tab):

1   C1  0   1   Out1    268 335 0
1   C1  0   2   Out2    0   0   0
1   C2  1   1   Out1    0   0   0
1   R3  1   1   Out2    317 0   0
1   P1  -1  all Sat1    268 0   335
1   C1  -1  all Tim1    0   0   0
1   R2  1   9   Out1    0   0   0

I need to extract 6th column data for those outputs which are "R3" in 2nd column and "Out2" in 5th column. In this example, the desired value is "317".

How can I extract this value (=317) in each second from the printed output in the stdout by C++?

BlueBit
  • 300
  • 1
  • 3
  • 17

2 Answers2

1

You don't want to use a regex for this, or anything, if you don't have to.

What you need to do is read in the file, split it by tabs and/or spaces, I can't tell, and then retrieve the element you want.

Read about how to split strings here: Split a string in C++?

to read from stdin, which is what it looks like you want to do, you need to do this:

std::string line;
std::getline(std::cin, line);

When combined with the string splitting method above, it will work wonderfully. You will then be able to ToolThatPrintsThings | YourNewTool

Community
  • 1
  • 1
Salgar
  • 7,687
  • 1
  • 25
  • 39
  • It seems fine. What does it mean by ToolThatPrintsThings | YourNewTool – BlueBit Nov 28 '13 at 10:13
  • `std::getline(std::cin, line)` waits for inputting my data in command line. But i need to capture data itself from auto-generated data in the `stdout`, not wait for inputting data by human. – BlueBit Nov 28 '13 at 13:54
0

Use awk:

ndnSIM | awk '{print $6}' | yourProg

Now yourProg just has to read each line with cin::getline(), which will be column 6 from the ndnSIM output.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I should use your suggested solution in the command line? such as this one: `./waf --run=MyProg | awk '{print $6}' | cin::getline()`? But i think i misunderstood! First pipe is the command of running ndnSIM simulator. – BlueBit Nov 28 '13 at 10:36
  • `cin::getline()` is something you write in your C++ program to read a line from standard input. It's not a shell command. – Barmar Nov 28 '13 at 10:40
  • What is the library of 'awk' to declare it? – BlueBit Nov 28 '13 at 11:43
  • It's not a library, it's a shell command. – Barmar Nov 28 '13 at 11:47
  • Don't you recognize a Unix shell pipeline? – Barmar Nov 28 '13 at 11:48
  • I think, it can also work in the program! I am not expert in C++. But, I need to extract column 6 or a specific data in column 6 and save it in a string in the program for further usage. How is it possible? – BlueBit Nov 28 '13 at 11:58
  • I don't understand why you're trying to make things harder for yourself. Use existing tools to simplify things. awk will extract column 6, then your C++ program only has to read a line from standard input. – Barmar Nov 28 '13 at 12:00