Debian Codegraph @

awklib/eg/prog/tee.awk

raw
1 # tee.awk --- tee in awk
2 #
3 # Copy standard input to all named output files.
4 # Append content if -a option is supplied.
5 #
6 # Arnold Robbins, [email protected], Public Domain
7 # May 1993
8 # Revised December 1995
9
10 BEGIN {
11 for (i = 1; i < ARGC; i++)
12 copy[i] = ARGV[i]
13
14 if (ARGV[1] == "-a") {
15 append = 1
16 delete ARGV[1]
17 delete copy[1]
18 ARGC--
19 }
20 if (ARGC < 2) {
21 print "usage: tee [-a] file ..." > "/dev/stderr"
22 exit 1
23 }
24 ARGV[1] = "-"
25 ARGC = 2
26 }
27 {
28 # moving the if outside the loop makes it run faster
29 if (append)
30 for (i in copy)
31 print >> copy[i]
32 else
33 for (i in copy)
34 print > copy[i]
35 print
36 }
37 END {
38 for (i in copy)
39 close(copy[i])
40 }