extracting "rest of line" in AWK

John Mundt john at chinet.chi.il.us
Sat Aug 26 04:45:19 AEST 1989


In article <3368 at blake.acs.washington.edu> mbader at cac.washington.edu (Mark Bader) writes:
>Does anyone know of a way to extract the "rest of the line" in awk..
>e.g. I have a line that looks like
>
>%% Heading "This is a Graph Heading"
>
>and I want to do a {print $3} to get the "This is a Graph Heading" part,
>but this obviously dosen't work.   Is there a way to do this?  

If I understand this correctly, and if the quotes come around the
heading all the time, you can use the split() function to break
the line up in to three substriings, namely

	1)  <%% Heading >
	2)  <This is a Graph Heading>
	3)  <>

by using split as in

	split($0,chop,"\"");	# I don't know if an escaped
				# quote will work or if you'd have to
				# change FS to it

You could then print the thing, with the quotes, by doing

	printf("\"%s\"\n",chop[2]);

If there are no quotes, you could still split it at every space
and print each separate array piece until you ran out of them:
(I assume that there are a variable number of words in the heading)

	split($0,chop);
	i = 3;
	while (chop[i] != "")
		printf("%s ",chop[i++]);
	printf("\n");
	for ( i = 0;; i++)	# zero out the array at the end
		chop[i] = "";	# after each use

The only problem here would be to allow for several spaces in a row
as in "This   is   a   heading".  I haven't tested split to see if
it would skip these null fields or not.
-- 
---------------------
John Mundt   Teachers' Aide, Inc.  P.O. Box 1666  Highland Park, IL
john at chinet.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  



More information about the Comp.unix.questions mailing list