Xref: utzoo comp.binaries.ibm.pc.d:9794 comp.sys.ibm.pc.misc:2443 Path: utzoo!utgpu!cunews!cognos!alzabo!tigris!glee From: glee@tigris.uucp (Godfrey Lee) Newsgroups: comp.binaries.ibm.pc.d,comp.sys.ibm.pc.misc Subject: Re: Is AWK up to this application? Keywords: awk Message-ID: <1990Oct7.223228.10387@tigris.uucp> Date: 7 Oct 90 22:32:28 GMT References: <1661@wjvax.UUCP> Reply-To: glee@tigris.UUCP (Godfrey Lee) Organization: Godfrey Lee Lines: 69 In article <1661@wjvax.UUCP> mario@wjvax.UUCP (Mario Dona) writes: >Does anyone know of a way of extracting information from a text file >which contains variable length fields, and outputting it in a different >format? Looks more like fixed length fields below. >For example, I have a text file which contains names and >addresses as shown below. > >John Doe 1563 Meadow Lane San Jose, CA 94325 >John Smith 345 N. First Street Oakland, CA 94356 >Henry & Martha Coe 4567 Poplar Ave. Newark, NJ 78389 > >^ ^ ^ >| | |_____ column 45 >| |______________________________ column 20 >|_________________________________________________ column 1 > >I want to extract the names and addresses and output it in the following >format: > >John Doe >1563 Meadow Lane >San Jose, CA 94325 > >John Smith >245 N. First Street >Oakland, CA 94356 > >Henry & Martha Coe >4567 Poplar Ave. >Newark, NJ 7838 > >Can this be done using AWK, and if so how? Or is there some other way? Simple, for example: awk -f address.awk outfile -------------- address.awk ---------------------------- { name = substr ( $0, 1, 19 ) add1 = substr ( $0, 20, 25 ) add2 = substr ( $0, 45 ) printf ( "%s\n%s\n%s\n\n", name, add1, add2 ) } ---------end of address.awk ---------------------------- Of course, it would be simpler if you just separate the fields with a single character such as ':', e.g. John Doe:1563 Meadow Lane:San Jose, CA 94325:more stuff ---> John Smith:345 N. First Street:Oakland, CA 94356:more stuff ---> Henry & Martha Coe:4567 Poplar Ave.:Newark, NJ 78389:more stuff---> then all you need is: awk -F: '{ printf ("%s\n%s\n%s\n\n", $1, $2, $3 ) }' outfile ^^^ | +---- defines field separator to be ':' Good luck. -- Godfrey Lee cognos!alzabo!tigris!glee