1

I noticed this interesting set of commands today:

$ seq 5 > alfa.txt
$ awk '{print 6 > ARGV[1]} 1' alfa.txt
1
2
3
4
5

$ cat alfa.txt
6
6
6
6
6

My first question was why am I getting several 6 rather than just one, but then I remembered you need to close each time:

awk '{print 6 > ARGV[1]; close(ARGV[1])} 1' alfa.txt

However what also puzzles me is if I am clobbering the input from the very beginning, how am I able to still go through and read the entire file? My guess is that Awk is actually writing to a buffer, then writing to the actual file at the end or perhaps every time the buffer fills.

If the latter is true, what is the buffer size?

1

It's implementation-dependent, but you're seeing awk open the output file after opening its input. The original input-file is lost, except for awk's open file-descriptor which it reads.

That's not just awk which could do this: it's common to many applications...

p.s: the buffer is your disk (size not determinable from the question).

0

At least on my system, it appears to be 32768 from a file, and 65536 from a pipe:

$ yes | head -100000 | tee file > pipe

$ awk '{print "n" > ARGV[1]}' file

$ sed s/y/n/ pipe | awk 'BEGIN {while (getline < "-") print > ARGV[1]}' pipe

$ wc -l file pipe
 32768 file
 65536 pipe
  • That's a different situation: your question did not use an example with pipes... – Thomas Dickey Aug 18 '18 at 16:10
  • @ThomasDickey I assumed it would be the same – Steven Penny Aug 18 '18 at 16:11
  • no... with the pipe, the shell gets involved in buffer-size. The original question did not actually use the shell's buffers. – Thomas Dickey Aug 18 '18 at 16:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.