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

  • Home
  • SEARCH
  • 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 6774453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:47:20+00:00 2026-05-26T15:47:20+00:00

awk newbie here.. I’m trying this: top -b -p 30259 | awk ‘BEGIN {

  • 0

awk newbie here..
I’m trying this:

top -b -p 30259 | awk 'BEGIN { OFS = ","; print "Timestamp,CPU,Memory"} /tomcat/ { print strftime("%H:%M:%S"), $9, $10 }' > asdf.log

But ‘asdf.log’ always stays empty. I tried redirecting to file from within the script:

top -b -p 30259 | awk 'BEGIN { OFS = ","; print "Timestamp,CPU,Memory" > "asdf.log"} /tomcat/ { print strftime("%H:%M:%S"), $9, $10 > "asdf.log" }'

but it still doesn’t work… And printing to stdout is working fine.

What am I doing wrong?

  • 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-05-26T15:47:20+00:00Added an answer on May 26, 2026 at 3:47 pm

    How long are you waiting for data to arrive?

    When programs use the standard C library to write to files (fopen(3) and family of functions), the C library will add some buffering to the streams to improve performance. If every read or write of one character actually started a system call, the performance of common programs would be pretty horrible. So the C standard input routines will submit blocks of IO when the buffers are “full enough”.

    The tricky parts comes that the C standard IO routines will change their definition of “full enough” based on the specific file descriptors — if the file descriptor is to a file, then the IO is block buffered, and you must wait for a certain amount of data to arrive. (Check stat asdf.log‘s IO Block entry to see the most likely size.) However, if the file descriptor is for stdout and it refers to a terminal device, the output is line buffered — output will be sent to the terminal as soon as a newline character is printed.

    Perhaps the setvbuf(3) manpage can explain better than I can:

       The three types of buffering available are unbuffered, block
       buffered, and line buffered.  When an output stream is
       unbuffered, information appears on the destination file or
       terminal as soon as written; when it is block buffered many
       characters are saved up and written as a block; when it is
       line buffered characters are saved up until a newline is
       output or input is read from any stream attached to a
       terminal device (typically stdin).  The function fflush(3)
       may be used to force the block out early.  (See fclose(3).)
       Normally all files are block buffered.  When the first I/O
       operation occurs on a file, malloc(3) is called, and a buffer
       is obtained.  If a stream refers to a terminal (as stdout
       normally does) it is line buffered.  The standard error
       stream stderr is always unbuffered by default.
    

    Amend your script to look like this:

    top -b | awk 'BEGIN { OFS = ","; print "Timestamp,CPU,Memory"}
    /bash/ { print strftime("%H:%M:%S"), $9, $10; fflush() }' > /tmp/foo
    

    The fflush() forces output immediately. Not great for performance, but better for viewing the state of your system in real time.

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

Sidebar

Related Questions

I have an AWK script which looks like #!/usr/bin/gawk -f BEGIN { print myVar;
Here is a query: grep bar 'foo.txt' | awk '{print $3}' The field name
Why this awk script: awk '{FS = \t ; print $1 - $2}' A.txt
Here is a simple command file * | awk '/ASCII text/ {gsub(/:/,,$1); print $1}'
I am trying to learn awk by doing some Project Euler questions. Here is
New to awk I am trying to print only the lines from a logfile
I find AWK really useful. Here is a one liner I put together to
I have an awk script that I have defined thus: #!/usr/bin/env awk BEGIN {
I know I can redirect awk's print output to another file from within a
I wrote the following awk to print lines from the match line until EOF

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.