9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* Re: [9fans] silly rc quoting question
@ 2006-12-18 12:05 erik quanstrom
  0 siblings, 0 replies; 4+ messages in thread
From: erik quanstrom @ 2006-12-18 12:05 UTC (permalink / raw)
  To: steve, 9fans

how about
	; eval 'a = (' `{cat junk} ')'
	; whatis a
	a=(f1 f2 'free text' f4)
	; echo $#a
	4

the reason that your original failed is that /bin/read returns one
line at a time.  the rc backquote operator then tokenizes not
according to rc's syntax rules but by $ifs.  the default for $ifs is
space, newline, tab.  thus the string
	f1 f2 'free text' f4newline
is parsed as
	"f1", "f2", "'free", "text'", f4

- erik

On Mon Dec 18 06:34:41 EST 2006, steve@quintile.net wrote:
> I have realised I don't understand rc's quoting
> rules as well as I thought I did. I want to parse
> the output of a program which has one record per line,
> and white space delimited fields in each line.
> 
> Some fields have whitespace in them but if they do then
> these fields are surrounded in quotes; here I will use cat
> and a file as an example, in "real life" its all a
> bit more complex.
> 
> 	term% cat junk
> 	f1 f2 'free text' f4
> 
> I expect when I parse this using the backquote operator then
> the quoting would be respected, this appears not to be
> the case:
> 
> 	term% cat junk | while(a=`{read}){
> 		echo $#a
> 		echo $a(3)
> 	}
> 	5
> 	'free
> 
> Where as I would expect:
> 
> 	4
> 	free text
> 
> I know I cannot do this with awk (unless I build the parser in awk
> (ugh)). Having said this I have been tempted to produce a non-standard,
> awk9 which understands plan9's unified quoting rules, even things like
> 
> 	ls -l | awk '{print $6, $10}'
> 
> are not reliable given whitespace in file names.
> 
> Is it possible to use rc to parse data using rc's rules?
> 
> -Steve


^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: [9fans] silly rc quoting question
@ 2006-12-18 12:30 erik quanstrom
  2006-12-18 15:23 ` Russ Cox
  0 siblings, 1 reply; 4+ messages in thread
From: erik quanstrom @ 2006-12-18 12:30 UTC (permalink / raw)
  To: steve, 9fans

Well, Don't Do That, Then. ☺

this awk script will do what you want
	ls -l|awk '{print substr($0, 38+length($4)+length($5));}'

it's slightly annoying, but unavoidable that ls -l's user and group
fields are dynamicly sized.

- erik


^ permalink raw reply	[flat|nested] 4+ messages in thread
* [9fans] silly rc quoting question
@ 2006-12-18 11:33 Steve Simon
  0 siblings, 0 replies; 4+ messages in thread
From: Steve Simon @ 2006-12-18 11:33 UTC (permalink / raw)
  To: 9fans

I have realised I don't understand rc's quoting
rules as well as I thought I did. I want to parse
the output of a program which has one record per line,
and white space delimited fields in each line.

Some fields have whitespace in them but if they do then
these fields are surrounded in quotes; here I will use cat
and a file as an example, in "real life" its all a
bit more complex.

	term% cat junk
	f1 f2 'free text' f4

I expect when I parse this using the backquote operator then
the quoting would be respected, this appears not to be
the case:

	term% cat junk | while(a=`{read}){
		echo $#a
		echo $a(3)
	}
	5
	'free

Where as I would expect:

	4
	free text

I know I cannot do this with awk (unless I build the parser in awk
(ugh)). Having said this I have been tempted to produce a non-standard,
awk9 which understands plan9's unified quoting rules, even things like

	ls -l | awk '{print $6, $10}'

are not reliable given whitespace in file names.

Is it possible to use rc to parse data using rc's rules?

-Steve


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-12-18 15:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-18 12:05 [9fans] silly rc quoting question erik quanstrom
  -- strict thread matches above, loose matches on Subject: below --
2006-12-18 12:30 erik quanstrom
2006-12-18 15:23 ` Russ Cox
2006-12-18 11:33 Steve Simon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).