Awk.Info

"Cause a little auk awk
goes a long way."

About awk.info
 »  table of contents
 »  featured topics
 »  page tags


About Awk
 »  advocacy
 »  learning
 »  history
 »  Wikipedia entry
 »  mascot
Implementations
 »  Awk (rarely used)
 »  Nawk (the-one-true, old)
 »  Gawk (widely used)
 »  Mawk
 »  Xgawk (gawk + xml + ...)
 »  Spawk (SQL + awk)
 »  Jawk (Awk in Java JVM)
 »  QTawk (extensions to gawk)
 »  Runawk (a runtime tool)
 »  platform support
Coding
 »  one-liners
 »  ten-liners
 »  tips
 »  the Awk 100
Community
 »  read our blog
 »  read/write the awk wiki
 »  discussion news group

Libraries
 »  Gawk
 »  Xgawk
 »  the Lawker library
Online doc
 »  reference card
 »  cheat sheet
 »  manual pages
 »  FAQ

Reading
 »  articles
 »  books:

WHAT'S NEW?

Mar 01: Michael Sanders demos an X-windows GUI for AWK.

Mar 01: Awk100#24: A. Lahm and E. de Rinaldis' patent search, in AWK

Feb 28: Tim Menzies asks this community to write an AWK cookbook.

Feb 28: Arnold Robbins announces a new debugger for GAWK.

Feb 28: Awk100#23: Premysl Janouch offers a IRC bot, In AWK

Feb 28: Updated: the AWK FAQ

Feb 28: Tim Menzies offers a tiny content management system, in Awk.

Jan 31: Comment system added to awk.info. For example, see discussion bottom of ?keys2awk

Jan 31: Martin Cohen shows that Gawk can handle massively long strings (300 million characters).

Jan 31: The AWK FAQ is being updated. For comments/ corrections/ extensions, please mail tim@menzies.us

Jan 31: Martin Cohen finds Awk on the Android platform.

Jan 31: Aleksey Cheusov released a new version of runawk.

Jan 31: Hirofumi Saito contributes a candidate Awk mascot.

Jan 31: Michael Sanders shows how to quickly build an AWK GUI for windows.

Jan 31: Hyung-Hwan Chung offers QSE, an embeddable Awk Interpreter.

[More ...]

Bookmark and Share

categories: Arrays,Apr,2009,Timm

saya

Synopsis

saya(array [,label,sep,before,after,eq])

Description

Array printing function. Contents printed, sorted on key.

Arguments

array
An array.
label
(OPTIONAL) A prefix before every item.
sep
(OPTIONAL) A string to print between each item. Defaults to new line.
before
(OPTIONAL) A string to print before the array. Defaults to "".
after
(OPTIONAL) A string to print after the array. Defaults to new line.
eq
(OPTIONAL) A string to print between each key/value pair. Defaults to " = ".

Returns

Size of the array

Notes

The most common usage is to just use the first two arguments; e.g.

saya(a,"name") ==>

name[1] = tim
name[2] = menzies

For other usages, see the examples, below.

Source

function saya(a,s, sep0,b4,after,eq,   c,m,n,key,val,i,j,tmp,sep) {
	sep0  = sep0  ? sep0  : "\n"
	b4    = b4    ? b4    : "\n"
	after = after ? after : "\n"
	eq    = eq    ? eq    : " = "
	pre   = s     ? s"["  : ""
	post  = s     ? "]"   : ""
	m     = asorti(a,b)
	printf("%s",b4)
	for(i=1;i<=m;i++)  {
		key=b[i]
		val=a[b[i]]
		printf("%s", sep pre  )
		n=split(key,tmp,SUBSEP)
		c = ""
		for(j=1;j<=n;j++)	{	
			printf("%s", c tmp[j]  )
			c=","
		}
		printf("%s", post eq val )
		sep=sep0;
	};
	printf("%s",after)
	return m
}

Example

gawk/array/eg/saya »

gawk -f saya.awk --source '
BEGIN { 	
	A["fname"  ] = "tim"
	A["lname"  ] = "menzies"
	A["address"] = "usa"
	saya(A,"",", ","[","]")
	print ""
	saya(A,"message")
	B[2,3,9]   = 100
	B[10,1,11] = 200
	B[1,3,10]  = 300
	saya(B,"b")
}'

gawk/array/eg/saya.out »

[address = usa, fname = tim, lname = menzies]

message[address] = usa
message[fname] = tim
message[lname] = menzies

b[1,3,10] = 300
b[10,1,11] = 200
b[2,3,9] = 100

Author

Tim Menzies


categories: Timm,Arrays,Function,Feb,2009,ArnoldR

join

Synopsis

join(a [,start,end,sep])

Description

Joins at array into a string

Arguments

a
input array
start
Index for where to start in the array a. Default=1.
end
Index for where to start/stop in the array a. Default=size of array
sep
(OPTIONAL) What to write between each item. Defaults to blank space.

If sep is set to the magic value SUBSEP then internally, join adds nothing between the items.

Returns

A string of a's contents.

Example

gawk/array/eg/join »

gawk -f join.awk --source '
BEGIN { split("tim tom tam",a)
        print join(a,2)
}'

gawk/array/eg/join.out »

tom tam

Source

function join(a,start,end,sep,    result,i) {
    sep   = sep   ? start :  " "
    start = start ? start : 1
    end   = end   ? end   : sizeof(a)
    if (sep == SUBSEP) # magic value
       sep = ""
    result = a[start]
    for (i = start + 1; i <= end; i++)
        result = result sep a[i]
    return result
}

Helper

In earlier gawks, length(a) did not work in functions. Hence....

function sizeof(a,   i,n) { for(i in a) n++ ; return n }

Change Log

  • Jan 24'08: defaults extended to include start,stop
  • Jan 24'08: Sizeof added to handle old gawk bug

Author

Arnold Robbins, then Tim Menzies


categories: Arrays,Function,Feb,2009,Admin

array

Synopsis

arrray(a)

Description

Ensure that an array is empty

Arguments

a
input array

Example

gawk/array/eg/array »

gawk -f array.awk --source '
BEGIN { array(A);
        A[1]=2;
	print length(A);
	array(A);
	print length(A);
}'

gawk/array/eg/array.out »

1
0

Source

function array(a) { split("",a,"") }
blog comments powered by Disqus