9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: erik quanstrom <quanstro@coraid.com>
To: steve@quintile.net, 9fans@cse.psu.edu
Subject: Re: [9fans] silly rc quoting question
Date: Mon, 18 Dec 2006 07:05:00 -0500	[thread overview]
Message-ID: <fcc66408f625921462ad0a49197ed66a@coraid.com> (raw)

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


             reply	other threads:[~2006-12-18 12:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-18 12:05 erik quanstrom [this message]
  -- 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fcc66408f625921462ad0a49197ed66a@coraid.com \
    --to=quanstro@coraid.com \
    --cc=9fans@cse.psu.edu \
    --cc=steve@quintile.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).