limited value in a File output

53 views Asked by At

I have a program that returns the execution time , the output of each run is diplayed in a file in the same row .

#include <iostream>
#include <fstream>
#include <math>
#include <boost>

using namespace std;

int main() {

std::ofstream file("MyFile.txt",std::ios_base::app);

v2 = rand() % 100 + 1; // just to burn  some time 
std::cout << v2 ;    // just to burn  some time


boost::chrono::duration<double> sec = boost::chrono::system_clock::now() - start;
    file<<sec.count() << ";";
file.close();
  return 0 ;
}

After 4 run here is the output in the file :

0.0190567;4.92035;11.0541;13.1457;

I would like that after 30 runs the 30'th result must be displayed in a newline . without any added functions or loops that will affect the duration of the runtime .I think that there is other way to format an output file via awk sed .

1

There are 1 answers

0
WaeCo On

You can use awk.

awk '{ gsub(/([0-9]+(.[0-9]*)?;){30}/, "&\n"); print }' MyFile.txt

This should insert a \n after each 30th ;