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


Reply via email to