9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: Mack Wallace <mackbw@mapinternet.com>
To: 9fans@9fans.net
Subject: [9fans] rc scripts, how to get spaces in array elements generated by command output?
Date: Sat,  5 Jan 2019 16:13:22 -0500	[thread overview]
Message-ID: <72B9C1DF-7927-4F58-9529-3DC122DEF47C@mapinternet.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 3012 bytes --]

I apologize if this is not the correct forum for this question. However, since I am trying to write a script in Plan 9’s rc shell, this seemed to be the best place to go - as most scripting resources are based on bash, and I could not find the answer in the rc references I could find. - and I’m rather new to scripting in general as it is.

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?

For example:

If I declare an array and output its contents with the following script:

declared_array = (‘hello world’ ‘good luck’)
echo $declared_array(1) 
echo $declared_array(2)

I will get:

hello world
good luck
 

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


The long version of what I am trying to do. 

I am trying to take a .CSV spreadsheet and make each row into a separate text file with the column header followed by the data for however many columns there are. The script will also make an index file of all files generated. This is so I can use Russ Cox’s Slide+ script to view each file, make changes, notes - and eventually use another script to put all the files back together into a new .CSV.

I already have a sed script that obfuscates extra quotes and commas of each record and returns a long string that’s comma delimited


The first task of the script is to read headers (first line) and store them to be used for each file. I want to anticipate that there may be spaces in individual headings. 


Ideally, I’d like to do this in one shot, with each heading being an individual element of an array. I tried changing the comma delimiters to various types of quotes in the sed script to keep headers with spaces together, but without success. So, to move on, I obfuscated the spaces and changed the commas to spaces. This allows the header row to be broken up appropriately, but then I still have to reverse the obfuscation.

header = `{read $1 | sed ’s/^/,/; s/\\”/☹/g; s/,”([^”]*)”/,☺\1☻/g; s/,”/,☺/; :MC; s/☺([^☻]*),([^☻]*)/☺\1☯\2/g; tMC; s/^,//; s/ /♪/g; s/,/ /g’}
(I quickly typed the script line, so there may be mistakes - the characters used for obfuscation will probably be \x02 ..\x05, however, I’m using unicode characters here as they are easier to see and debug.)


I could run sed for each header every time I generate a file, but that seems quite redundant if I can just run sed for the header once and save the result in a manner that is easily accessible. 

This is my first foray into scripting, so I am not sure what is good practice and form. Any suggestions are appreciated.

Thanks and best regards.

Mack



[-- Attachment #2: Type: text/html, Size: 5975 bytes --]

             reply	other threads:[~2019-01-05 21:13 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-05 21:13 Mack Wallace [this message]
2019-01-05 23:52 ` Anthony Martin
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=72B9C1DF-7927-4F58-9529-3DC122DEF47C@mapinternet.com \
    --to=mackbw@mapinternet.com \
    --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).