From: Anthony Martin <ality@pbrane.org>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] rc scripts, how to get spaces in array elements generated by command output?
Date: Sat, 5 Jan 2019 15:52:28 -0800 [thread overview]
Message-ID: <20190105235228.GA21088@alice> (raw)
In-Reply-To: <72B9C1DF-7927-4F58-9529-3DC122DEF47C@mapinternet.com>
Mack Wallace <mackbw@mapinternet.com> once said:
> My question: Is there a way to take the string output of a command that
> contains spaces and make it a single element of an array in rc script?
>
> [...]
>
> However, if I am receiving output from sed that contains spaces from
> the following script line
>
> string_var = `{echo some_string | sed ’s/x/y/g’}
>
> If the output was ‘hello world’, string_var would become a two
> element array which
> echo $some_string(1)
> echo $some_string(2)
>
> Would output
> hello
> world
You need to change the input field separator, $ifs.
The default value is any whitespace character:
% x = `{echo -n hello world}
% echo $#x
2
% echo $x(1)
hello
% echo $x(2)
world
Using only newline as the separator yields:
% ifs = '
' # this is a newline character
% y = `{echo -n hello world}
% echo $#y
1
% echo $y(1)
hello world
You might want to use a comma instead:
% ifs = ,
% z = `{echo -n hello world,good luck}
% echo $#z
2
% echo $z(1)
hello world
% echo $z(2)
good luck
Cheers,
Anthony
next prev parent reply other threads:[~2019-01-05 23:52 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-05 21:13 Mack Wallace
2019-01-05 23:52 ` Anthony Martin [this message]
2019-01-06 1:05 ` Mack Wallace
2019-01-06 14:34 ` Ethan Gardener
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=20190105235228.GA21088@alice \
--to=ality@pbrane.org \
--cc=9fans@9fans.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).