[comp.unix.questions] AWK field seperator question

75046.606@CompuServe.COM (Larry W. Virden) (02/15/90)

I have an awk question.  I am using SunOS 4.0.2 on a Sun 386i.
I want to , within a particular pattern { action }, reset the
Field Seperator, process the current record, then set the FS back
to the default action.

To do this, I tried:
/config/ {
        FS = "\""; print $2 ;
        FS = ""
        next
}

{
        print $1, $2
}

But, when I try this out, it doesnt perform as expected.  I do
get fields 1 and 2 if I dont have a record with config in it.  I do get info without
double quotes if
I type config "this is a test" to it.
But, if I then type another plain line, I don't get back the default
action.

Also, Certain things in the pattern field cause the behavior to change
- like trying to use a field variable in the patter (e.g. $1 ~ /config/).

Can anyone help me?  P{.S.  I have tried this in the version of gawk that I have
anbd it has similar problems there.

starr@mrsvr.UUCP (Larry Starr,Mezzanine,46971,5638828) (02/16/90)

From article <"900215154914.75046.606.CHD39-1"@CompuServe.COM>, by 75046.606@CompuServe.COM (Larry W. Virden):
> I have an awk question.  I am using SunOS 4.0.2 on a Sun 386i.
> I want to , within a particular pattern { action }, reset the
> Field Seperator, process the current record, then set the FS back
> to the default action.
> 
> [ stuff about previous attempts deleted ]

Why not try something straight forward ( and supported )

	n = split($0,x,"\"")
	print x[2]

This splits the input record around the quote into aray x
you might want to check n to see how many fields resulted
etc.

The FS variable is used when awk reads the record so the
result of your first attempt is to set FS for the next
record ( not the current one).

-L. Starr
--
Larry Starr
MR Research Facility
G.E. Medical Systems

paul@alice.UUCP (Paul Krzyzanowski) (02/16/90)

In article <"900215154914.75046.606.CHD39-1"@CompuServe.COM>, 75046.606@CompuServe.COM (Larry W. Virden) writes:
> I want to , within a particular pattern { action }, reset the
> Field Seperator, process the current record, then set the FS back
> to the default action.
> 
> To do this, I tried:
> /config/ {
>         FS = "\""; print $2 ;
>         FS = ""
>         next
> }
> 
> {
>         print $1, $2
> }

The program isn't doing what you want because you aren't "resetting"
the field separator.  Setting FS to an empty string doesn't reset
FS to its previous value;  you have to explicitely set it to use
white space as a separator.

Try           FS = " "
instead of    FS = ""


	-Paul Krzyzanowski
	 paul@allegra.att.com

matthew@sunpix.East.Sun.COM ( Sun Visualization Products) (02/18/90)

In article <"900215154914.75046.606.CHD39-1"@CompuServe.COM> 75046.606@CompuServe.COM (Larry W. Virden) writes:
]I have an awk question.  I am using SunOS 4.0.2 on a Sun 386i.
]I want to , within a particular pattern { action }, reset the
]Field Seperator, process the current record, then set the FS back
]to the default action.
]
]To do this, I tried:
]/config/ {
]        FS = "\""; print $2 ;
]        FS = ""
]        next
]}
]
]{
]        print $1, $2
]}

    Leave the field seperator alone, and try using the split function.

                            n = split(s, arr, sep)

n = number of resultant elements
s = the string to be disected
arr = the name of the array to place the elements
sep = the seperation character

    In your example I would do:

 /config/ { split($0, LINE, "\"); print LINE[2] }



-- 
Matthew Lee Stier                            |
Sun Microsystems ---  RTP, NC  27709-3447    |     "Wisconsin   Escapee"
uucp:  sun!mstier or mcnc!rti!sunpix!matthew |
phone: (919) 469-8300 fax: (919) 460-8355    |