Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:28:28+00:00 2026-06-07T17:28:28+00:00

I have a program which will be running continuously writing output to stdout, and

  • 0

I have a program which will be running continuously writing output to stdout, and I would like to continuously take the last set of output from it and write this to a file. Each time a new set of output comes along, I’d like to overwrite the same file.

An example would be the following command:

iostat -xkd 5

Every 5 seconds this prints some output, with a blank line after each one. I’d like the last “set” to be written to a file, and I think this should be achievable with something simple.

So far I’ve tried using xargs, which I could only get to do this with a single line, rather than a group of lines delimited by something.

I think it might be possible with awk, but I can’t figure out how to get it to either buffer the data as it goes along so it can write it out using system, or get it to close and reopen the same output file.

EDIT: To clarify, this is a single command which will run continuously. I do NOT want to start a new command and read the output of that.

SOLUTION

I adapted the answer from @Bittrance to get the following:

iostat -xkd 5 | (while read r; do
        if [ -z "$r" ]; then
                mv -f /tmp/foo.out.tmp /tmp/foo.out
        else
                echo "$r" >> /tmp/foo.out.tmp
        fi
done)

This was basically the same, apart from detecting the end of a “section” and writing to a temp file so that whenever an external process tried to read the output file it would be complete.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:28:30+00:00Added an answer on June 7, 2026 at 5:28 pm

    Partial answer:

    ./your_command | (while read r ; do 
        if ... ; then
            rm -f foo.out
        fi
        echo $r >> foo.out
    done)
    

    If there is a condition (the …) such that you know that you are receiving the first line of the “set” this would work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app will be running from a USB key which have specific information like
I have a program which I would like to run every X min the
I have a program which takes a long time to complete. I would like
I am writing a program which will preform texture synthesis. I have been away
How to write a java program which will tell me whether I have internet
I have a C++ program which, during execution, will allocate about 3-8Gb of memory
I have an application which will start a program flow with the arrival of
I am writing a c++ program which sooner or later will need to be
I have a program which executes sql statements. Within a transaction, I'd like to
I'm writing a C++ program for a Windows 7 user which will be the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.