From mboxrd@z Thu Jan 1 00:00:00 1970 From: Mack Wallace Content-Type: multipart/alternative; boundary="Apple-Mail=_12D320D5-859D-42FF-AEC5-2D58CA56A43D" Mime-Version: 1.0 (Mac OS X Mail 12.1 \(3445.101.1\)) Message-Id: <72B9C1DF-7927-4F58-9529-3DC122DEF47C@mapinternet.com> Date: Sat, 5 Jan 2019 16:13:22 -0500 To: 9fans@9fans.net Subject: [9fans] rc scripts, how to get spaces in array elements generated by command output? Topicbox-Message-UUID: f2742ec4-ead9-11e9-9d60-3106f5b1d025 --Apple-Mail=_12D320D5-859D-42FF-AEC5-2D58CA56A43D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 I apologize if this is not the correct forum for this question. However, = since I am trying to write a script in Plan 9=E2=80=99s 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=E2=80=99m 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 =3D (=E2=80=98hello world=E2=80=99 =E2=80=98good luck=E2=80= =99) echo $declared_array(1)=20 echo $declared_array(2) I will get: hello world good luck =20 However, if I am receiving output from sed that contains spaces from the = following script line string_var =3D `{echo some_string | sed =E2=80=99s/x/y/g=E2=80=99} If the output was =E2=80=98hello world=E2=80=99, string_var would become = a two element array which echo $some_string(1)=20 echo $some_string(2) Would output=20 hello world The long version of what I am trying to do.=20 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=E2=80=99s 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=E2=80=99s 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.=20 Ideally, I=E2=80=99d 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 =3D `{read $1 | sed =E2=80=99s/^/,/; s/\\=E2=80=9D/=E2=98=B9/g; = s/,=E2=80=9D([^=E2=80=9D]*)=E2=80=9D/,=E2=98=BA\1=E2=98=BB/g; = s/,=E2=80=9D/,=E2=98=BA/; :MC; s/=E2=98=BA([^=E2=98=BB]*),([^=E2=98=BB]*)/= =E2=98=BA\1=E2=98=AF\2/g; tMC; s/^,//; s/ /=E2=99=AA/g; s/,/ /g=E2=80=99} (I quickly typed the script line, so there may be mistakes - the = characters used for obfuscation will probably be \x02 ..\x05, however, = I=E2=80=99m 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.=20 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 --Apple-Mail=_12D320D5-859D-42FF-AEC5-2D58CA56A43D Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8 I = apologize if this is not the correct forum for this question. However, = since I am trying to write a script in Plan 9=E2=80=99s 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=E2=80=99m 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 =3D (=E2=80=98hello world=E2=80=99 =E2=80=98good= luck=E2=80=99)
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 = =3D `{echo some_string | sed =E2=80=99s/x/y/g=E2=80=99}

If the output was = =E2=80=98hello world=E2=80=99, 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=E2=80=99s 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=E2=80=99s 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=E2=80=99d = 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 =3D `{read = $1 | sed =E2=80=99s/^/,/; s/\\=E2=80=9D/=E2=98=B9/g; = s/,=E2=80=9D([^=E2=80=9D]*)=E2=80=9D/,=E2=98=BA\1=E2=98=BB/g; = s/,=E2=80=9D/,=E2=98=BA/; :MC; = s/=E2=98=BA([^=E2=98=BB]*),([^=E2=98=BB]*)/=E2=98=BA\1=E2=98=AF\2/g; = tMC; s/^,//; s/ /=E2=99=AA/g; s/,/ = /g=E2=80=99}
(I quickly typed the script line, so there may be mistakes - = the characters used for obfuscation will probably be \x02 ..\x05, = however, I=E2=80=99m 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


= --Apple-Mail=_12D320D5-859D-42FF-AEC5-2D58CA56A43D--