[comp.unix.questions] extracting "rest of line" in AWK

mbader@cac.washington.edu (Mark Bader) (08/25/89)

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 there were a shift command, then I could shift off the first 2 fields,
but is there a way to simulate this?

Thanks in advance,

Mark Bader                               INTERNET:  mbader@cac.washington.edu
Networks and Distributed Computing, UW     BITNET:  mbader@uwav1
3737 Brooklyn Ave. NE                        BELL:  (206) 543-5128
Seattle, WA  98105                          
	

davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) (08/25/89)

  You can get what you want by using substring to extract from $0.
-- 
bill davidsen	(davidsen@crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon

karish@forel.stanford.edu (Chuck Karish) (08/25/89)

In article <3368@blake.acs.washington.edu> mbader@cac.washington.edu
(Mark Bader) wrote:
>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?  

{for (num = 3; num <= NF; num++) {printf "%s ", $num}}
{printf "\n"}

	Chuck Karish		karish@mindcraft.com
	(415) 493-9000		karish@forel.stanford.edu

jaap+@andrew.cmu.edu (Jaap Akkerhuis) (08/26/89)

> Excerpts from netnews.comp.unix.questions: 25-Aug-89 extracting "rest of
> line" i.. Mark Bader@cac.washingto (670)

> 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 there were a shift command, then I could shift off the first 2 fields,
but is there a way to simulate this?

Allthough it is a bit ugly, you can empty the first two fields, so
something like:

	{ $1="\0"; $2="\0"; print $0 }

Of course, now you will have two spaces in front of the line printed. If
you want to get rid of this you need a for loop in the style of "{ for(i
= 3; i < NF; i++) printf("%s ", $i); print ""}" or just filter them out
with an "awk '{ $1=""; $2=""; print $0 }' | sed 's/^  //'".

There are tons of other ways to do this as well.


	jaap

john@chinet.chi.il.us (John Mundt) (08/26/89)

In article <3368@blake.acs.washington.edu> mbader@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@chinet.chi.il.us
(312) 998-5007 (Day voice) || -432-8860 (Answer Mach) && -432-5386 Modem  

steinbac@hpl-opus.HP.COM (Gunter Steinbach) (08/26/89)

How about      '{$1=$2=""; print}'  ?

Yes, I admit you get two extra field separators before the "rest".

        Guenter Steinbach                | hplabs!hpl-opus!gunter
                                         | gunter%hpl-opus@hp-sde.hp.com

dg@lakart.UUCP (David Goodenough) (08/27/89)

mbader@cac.washington.edu (Mark Bader) asks:
> 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?  

It's fairly grotesque, a lot of work, but the following should work,
assuming you're not to worried about spaces.

	{ for (i = 3; i <= NF; i++) {
			if (i == NF)
			 {
			    a = "\n";
			 }
			else
			 {
			    a = " ";
			 }
			printf "%s%s", $i, a; } }

Or somesuch. I'm making the above up as I type here, so I can't guarantee it,
but it should provide a starting point. An alternative approah is to use
length($1) and length($2) and substr(), but again that assumes a lot about
the input record.

Enough rambling, I'm not really an awk guru. We now return you to your
regular news articles.
-- 
	dg@lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp@xait.xerox.com			  +---+

davr@hrtix.UUCP (David C. Raines) (08/28/89)

In article <9363@chinet.chi.il.us>, john@chinet.chi.il.us (John Mundt) writes:
> In article <3368@blake.acs.washington.edu> mbader@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?  

How about:

	for (v = 3; v <= NF; v++)
		printf ("%s ", $v)
	printf "\n"

or alternatively (if the quotes are always present), set FS='"' and print $2.

-- 
--					TCA
David Raines				5 National Dr. 
UUCP:  ...!uunet!hrtix!davr		Windsor Locks, CT 06096

poage@sunny.ucdavis.edu (Tom Poage) (08/31/89)

To extract the third argument from

	%% Heading "This is a Graph Heading"

try

	/^%% Heading/ {
		n = split($0, array, "\"")
		print array[2]
	}

which will produce

	This is a Graph Heading

Tom.
-- 
Tom Poage, Clinical Engineering
Universiy of California, Davis, Medical Center, Sacramento, CA
poage@sunny.ucdavis.edu  {...,ucbvax,uunet}!ucdavis!sunny!poage

jon@jonlab.UUCP (Jon H. LaBadie) (09/05/89)

In article <671@lakart.UUCP>, dg@lakart.UUCP (David Goodenough) writes:
> mbader@cac.washington.edu (Mark Bader) asks:
> > 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?  
> 
> It's fairly grotesque, a lot of work, but the following should work,
> assuming you're not to worried about spaces.
> 

SOLUTION DELETED

New awk added a substitute function that can get rid of unwanted fields
whild preserving the spacing in the rest of the record.  For example,

	sub($1, "", $0)

should get rid of field on while leaving the remainder of the record
intact (including spacing).  Repeating the above command should get
rid of field one and two.  An alternative, in one statement is

	sub($1 "[ 	]*" $2 "[ 	]*", "", $0)

where each set of square brackets contains a tab and a space.

Remember to preserve $1 and $2 if the logic of the program requires
them at a later point in the code.

Jon LaBadie
-- 
Jon LaBadie
{att, princeton, bcr}!jonlab!jon
{att, attmail, bcr}!auxnj!jon

bob@wyse.wyse.com (Bob McGowen Wyse Technology Training) (09/07/89)

In article <4831@portia.Stanford.EDU> karish@forel.stanford.edu (Chuck Karish) writes:
---deleted---
>>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?  
>
>{for (num = 3; num <= NF; num++) {printf "%s ", $num}}
>{printf "\n"}

Or:

awk -F\" '{print $2}'

which sets the field separator to " and then prints the second field, which
is between the two quotes.  If you need the quotes output then the print
could be:

	print "\""$2"\""

This will guarantee that everything between the quotes will be printed,
while anything that may follow the second quote will be ignored.  Also,
no assumptions are made about the number of leading fields so modification
of the data file will be less likely to require changing of the the
awk script.

Bob McGowan  (standard disclaimer, these are my own ...)
Customer Education, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com