summaryrefslogtreecommitdiffstats
path: root/contrib/awk/awklib/eg/prog/split.awk
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/awk/awklib/eg/prog/split.awk')
-rw-r--r--contrib/awk/awklib/eg/prog/split.awk53
1 files changed, 53 insertions, 0 deletions
diff --git a/contrib/awk/awklib/eg/prog/split.awk b/contrib/awk/awklib/eg/prog/split.awk
new file mode 100644
index 0000000..863ba4e
--- /dev/null
+++ b/contrib/awk/awklib/eg/prog/split.awk
@@ -0,0 +1,53 @@
+# split.awk --- do split in awk
+# Arnold Robbins, arnold@gnu.org, Public Domain
+# May 1993
+
+# usage: split [-num] [file] [outname]
+
+BEGIN {
+ outfile = "x" # default
+ count = 1000
+ if (ARGC > 4)
+ usage()
+
+ i = 1
+ if (ARGV[i] ~ /^-[0-9]+$/) {
+ count = -ARGV[i]
+ ARGV[i] = ""
+ i++
+ }
+ # test argv in case reading from stdin instead of file
+ if (i in ARGV)
+ i++ # skip data file name
+ if (i in ARGV) {
+ outfile = ARGV[i]
+ ARGV[i] = ""
+ }
+
+ s1 = s2 = "a"
+ out = (outfile s1 s2)
+}
+{
+ if (++tcount > count) {
+ close(out)
+ if (s2 == "z") {
+ if (s1 == "z") {
+ printf("split: %s is too large to split\n", \
+ FILENAME) > "/dev/stderr"
+ exit 1
+ }
+ s1 = chr(ord(s1) + 1)
+ s2 = "a"
+ } else
+ s2 = chr(ord(s2) + 1)
+ out = (outfile s1 s2)
+ tcount = 1
+ }
+ print > out
+}
+function usage( e)
+{
+ e = "usage: split [-num] [file] [outname]"
+ print e > "/dev/stderr"
+ exit 1
+}
OpenPOWER on IntegriCloud