How to make arrays from strings in bash?

Hey all,

This is my first post, and I am relatively new to linux/unix scripts. I am writing a bash script in which I am trying to extract one line from another file and parse specific words from the line into an array. So for example, I have a file called SortScans in which the first 5 lines might look like this (nevermind that this file is in csh):

 
#!/bin/csh

set Runs = (2 3 4 5 6 7 8 9 10 11 12 13)
set Folders = (anat RS1 RS2 RS3 DTI DTI DTI DTI DTI DTI DFM DFM)

and in my new script, I want to create 2 arrays that would correspond just to what's inside those parentheses, where the end result would be the following 2 arrays

RunsArray
[] = 2 3 4 5 6 7 8 9 10 11 12 13
FoldersArray
[
] = anat RS1 RS2 RS3 DTI DTI DTI DTI DTI DTI DFM DFM

So here is the code that I am using so far in my script just to create RunsArray

 
#!/bin/bash

RUNS=`echo | awk 'NR==4 {print;exit}' SortScans`

sed -i 's/*(//' $RUNS
sed -i 's/)//' $RUNS

declare -a RunsArray
RunsArray=$RUNS

I don't really know what I'm doing there, I just sort of copy pasted different related things I've seen on forums. But I'm getting the following error on the sed part -

sed: can't read set: no such file or directory

and that error repeats for each word or number down the line (i.e. runs, =, (2, 3, 4, etc.)

Anybody think they can help me out? Like I said I'm just starting out, so if you think you have a better way of doing this altogether, I would love to hear it. Thanks y'all.

This should do it:

#!/bin/bash
RunsArray=( $(sed -n '/set Runs/{s/.*(//; s/)$//p}' SortScans) )
 
echo RunsArray elements: ${#RunsArray[@]}
echo Number 0: ${RunsArray[0]}
echo Number 1: ${RunsArray[1]}
1 Like

Hi,

I don't know why, but my script doesn't print anything.

RunsArray = ( $(awk -F"(" '/set Runs/{sub(/\)/,"");print $2}' SortScans) )
FoldersArray = ( $(awk -F"(" '/set Folders/{sub(/\)/,"");print $2}' SortScans) )

echo  ${RunsArray[@]}
echo  ${FoldersArray[@]}

Maybe somebody could see what is wrong.

Thanks in advance

Regards

It's that spaces on either side of "="
Also you cat get rid of the sub() call my making field seperator "(" or ")"

RunsArray=( $(awk -F"[)(]" '/set Runs/{print $2}' SortScans) )
FoldersArray=( $(awk -F"[)(]" '/set Folders/{print $2}' SortScans) )
 
echo  ${RunsArray[@]}
echo  ${FoldersArray[@]}
1 Like

Thanks very much, I will try that. But will this work without the final forward slash for the sed command - after the } and before the '? Could you explain why it isn't needed there?

First lets expand the sed command to make it more readable:

sed -n '
/set Runs/ {
    s/.*(//;
    s/)$//p
}' SortScans

This does don' t print any line by default (-n).
For any line that contains "set Runs":

  • Replace everything up to last ( with nothing
  • Replace ) on end of line with nothing and then print result
1 Like

Hey thanks, it works great for FoldersArray (creates an array of strings) but it doesn't work for RunsArray (which would be an array of numbers).

RunsArray just comes up as an empty array. Any ideas?

What is the output of:

sed -n '/set Runs/{s/.*(//; s/)$//p}' SortScans

What is the format of SortScans?

---------- Post updated at 09:52 PM ---------- Previous update was at 09:51 PM ----------

OOPS, my mistake on testing your script. It works great!! Thanks so much.

Thanks Chubler,

I was close:p. My original code, this time a working code is:

RunsArray=( $(awk -F"(" '/set Runs/{sub(/\)/,"");print $2}' SortScans) )
FoldersArray=( $(awk -F"(" '/set Folders/{sub(/\)/,"");print $2}' SortScans) )

echo  ${RunsArray[@]}
echo  ${FoldersArray[@]}

And compressed changing field separator with your suggestion is a you said before:

RunsArray=( $(awk -F"[)(]" '/set Runs/{print $2}' SortScans) )
FoldersArray=( $(awk -F"[)(]" '/set Folders/{print $2}' SortScans) )

echo  ${RunsArray[@]}
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
echo  ${FoldersArray[@]}
anat RS1 RS2 RS3 DTI DTI DTI DTI DTI DTI DFM DFM

Thanks for your correction!:b:

Regards