954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Use bash to call c++ program in a loop

I am trying to do this

#!/bin/bash

for i in 1 2 3 4 5; do 
	File1="$i.txt"
	File2="$i.obj"
	./Test $File1 $File2
 #save i and the result of the program somehow
done;


For example, at the end I would like to have a file like this

1 3.4
2 4.55
3 56.0
4 22.3
5 8.9


Where the second column is the "output" of the c++ program. The problem is the program has other outputs, ie

Starting program....
Doing stuff ....
3.4


Anyone ever wrap a program in a loop like this before and care about saving the output?

Thanks,
Dave

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

Filter the output with grep:
./Test "$i.txt" "$i.obj" | grep -vi '^[:alpha:]'


Perl's good for this:

for $i (1..5) {
  for (`./Test $i.txt $i.obj`) {
    next if /^\s*\w/;
    print;
  }
}
nucleon
Posting Pro in Training
478 posts since Oct 2008
Reputation Points: 163
Solved Threads: 91
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You