Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!zaphod.mps.ohio-state.edu!ncar!mephisto!mcnc!rti!dg-rtp!dg-rtp.dg.com!rice From: rice@dg-rtp.dg.com (Brian Rice) Newsgroups: comp.unix.questions Subject: Re: Awk Field Separators Summary: the low rent way Message-ID: <1990Aug29.173556.15121@dg-rtp.dg.com> Date: 29 Aug 90 17:35:56 GMT References: <3729@se-sd.SanDiego.NCR.COM> <427@necssd.NEC.COM> Sender: usenet@dg-rtp.dg.com (Usenet Administration) Reply-To: rice@dg-rtp.dg.com Followup-To: comp.unix.questions Organization: Data General Corporation, Research Triangle Park, NC Lines: 60 In article <427@necssd.NEC.COM>, harrison@necssd.NEC.COM (Mark Harrison) writes: |> In article <3729@se-sd.SanDiego.NCR.COM>, cubbage@se-sd.SanDiego.NCR.COM |> (Sharon Cubbage) 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 : |> |> The new version of Awk (nawk) can do this, but the old version can't. If you don't have nawk (you're missing out if you don't!), you can achieve the same effect by writing a filter program, like so (this is off the top of my head): #include #include #define MY_DELIMITER_CHARS " |\n" #define MY_OUTPUT_DELIMITER ' ' #define MY_BUFSIZE 256 /* Use the getline() on page 67 of K&R1. */ main() { char *strtok(); char buff[MY_BUFSIZE]; char *token; while (getline(buff,MY_BUFSIZE) != 0) { token = strtok(buff,MY_DELIMITER_CHARS); while (token != NULL) { fputs(token,stdout); putchar(MY_OUTPUT_DELIMITER); token = strtok(NULL,MY_DELIMITER_CHARS); } putchar('\n'); } } Compile and link, then use it like this: $ echo "1 2 3 4|5|6" | the_above_program | awk '{print $5}' 5 A nice enhancement would be to allow the user to specify a list of input delimiter characters and the output delimiter character on the command line (but make sure that newline is an input delimiter, unless you want to change the code). -- Brian Rice rice@dg-rtp.dg.com +1 919 248-6328 DG/UX Product Assurance Engineering Data General Corp., Research Triangle Park, N.C.