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 8036291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:34:15+00:00 2026-06-05T02:34:15+00:00

echo bla | awk ‘BEGIN{fname=foo.txt} {print $0 >>fname; print rm -f fname | /usr/bin/ksh;

  • 0
echo bla | awk 'BEGIN{fname="foo.txt"} {print $0 >>fname; print "rm -f " fname | "/usr/bin/ksh"; close("/usr/bin/ksh"); print $0 >>fname}'

After this command has executed, I should end up with a file “foo.txt”, right ?

It doesn’t work. I tried system(“/usr/bin/rm -f ” fname) to remove the file, bash instead of ksh, Linux, HP-UX, Cygwin, fflush(“”),… just doesn’t work !
It seems after a file is deleted, Awk just can’t write to a file with the same name anymore.

Looks like a bug in Awk, or am I missing something (big time !) ?!

  • 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-05T02:34:18+00:00Added an answer on June 5, 2026 at 2:34 am

    awk is keeping the file open. Try closing it:

    echo bla | awk 'BEGIN{fname="foo.txt"} 
    {
        print $0 >>fname 
        print "rm -f " fname | "/usr/bin/ksh"
        close("/usr/bin/ksh")
        close( fname ) 
        print $0 >>fname
    }'
    

    awk opens the file the first time you write to it, and keeps that file open. When the link is removed from the filesystem, awk still has the file open so it doesn’t use the “foo.txt” name to access it. By closing the file, you force awk to look at the filesystem again and create the now non-extant link “foo.txt”.

    For the benefit of readers who do not understand the distinction between a link and a file, try the following:

    $ rm -rf /tmp/foo; mkdir /tmp/foo; cd /tmp/foo  # start with a clean directory
    $ touch foo.txt; ln foo.txt bar.txt             # Create a file with two links
    $ # run original awk script (without closing the file)
    $ cat bar.txt
    

    You will see the line “bla” twice in bar.txt. The reason is that bar.txt and foo.txt are both links to the same file. awk opens that file and writes a line to it, then deletes the link foo.txt, then writes another line to the file. When awk terminates, the link foo.txt has been deleted, but the file is still there and accessible through the link bar.txt. If bar.txt is deleted, then the filesystem will notice that the link count for the file has dropped to zero and the file will be deleted.

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

Sidebar

Related Questions

Let's say I've got a shell script called print_error.sh looking like this: #!/usr/bin/bash echo
echo -e 55 11\n25 11.0 | awk '$2 ~ /11/{print $1}' I only want
echo preg_replace(/f(o)o/, 'b$1r', 'This is foo.'); $var = ??? // here I want to
echo date('H:i', time()); // 10:15 echo date('H:i', strtotime($this->deadline)); // 10:05 $delay = time() -
echo date(W,strtotime('2010-01-01')); This outputs 53. I would expect it to output 1. Can anyone
echo '<img src=data:image/jpg/png/jpeg;base64,' . base64_encode( $row['image'] ) . ' height=150 />'; This is showing
PHP Echo or Print functions does not show anything when php is busy with
I am trying to do something like this: //function name $str = 'bla()'; //make
echo 1 . (print '2') + 3; returns 214. How does the script end
echo $nav gives code like this: <ul> <li class=someclass>sometext <ul> <li class=someclass>sometext</li> <li class=spacer></li>

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.