Awk Field Separators

Adam Bryant adb at cs.bu.edu
Sun Aug 26 07:21:09 AEST 1990


In article <3353 at mwk.uucp> wrightgr at mwk.uucp (Greg, Ext. 3414) writes:
+  Does anybody know how to specify more than one field separator in Awk?
+  I would like to specify to an Awk program to treat single spaces as well
+  as bars as field separators so that a string such as :
+  
+  12 12 12 34|34|34
+  
+  will be said to have 6 fields.  I've tried to create a regular expression
+  to handle both cases but it hasn't been working.

I use the split() command when handling multiple field separators.

Code to print a list of fields separated by a ' ' or a '|' is:

{
     for (i = 1; i <= NF; i++) {
	num = split($i, nstr, "|");
	for (j = 1; j <= num; j++) {
		print( nstr[j] )
	}
     }
}

Hope this helps.

adam bryant



More information about the Comp.unix.questions mailing list