How to make arrays from strings in bash?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make arrays from strings in bash?
# 1  
Old 03-30-2011
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):

Code:
 
#!/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

Code:
 
#!/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.
# 2  
Old 03-30-2011
This should do it:

Code:
#!/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]}

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 03-31-2011
Hi,

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

Code:
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
# 4  
Old 03-31-2011
It's that spaces on either side of "="
Also you cat get rid of the sub() call my making field seperator "(" or ")"
Code:
RunsArray=( $(awk -F"[)(]" '/set Runs/{print $2}' SortScans) )
FoldersArray=( $(awk -F"[)(]" '/set Folders/{print $2}' SortScans) )
 
echo  ${RunsArray[@]}
echo  ${FoldersArray[@]}

This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 03-31-2011
Quote:
Originally Posted by Chubler_XL
This should do it:

Code:
#!/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]}

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?
# 6  
Old 03-31-2011
First lets expand the sed command to make it more readable:

Code:
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
This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 04-01-2011
Quote:
Originally Posted by Chubler_XL
This should do it:

Code:
#!/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]}

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?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Arrays of strings in GFORTRAN errors

Hello I am using gfortran and I intended to do thiis: Module variables character(len=:), dimension(:), allocatable, array end module variables Sub test use variables integer (max_len) max_len=len_trim("something here") if(.not.allocated(array))... (1 Reply)
Discussion started by: pepe
1 Replies

2. Shell Programming and Scripting

Using arrays in bash using strings to bash built-in true

I have the following code and for some reason when I call the program using /home/tcdata/tatsh/trunk/hstmy/bin/bash/raytrac.bash --cmod=jcdint.cmod I get hasArgument = hasArgument = true Somehow the array element is returning even though I have not chosen the option. ... (41 Replies)
Discussion started by: kristinu
41 Replies

3. Shell Programming and Scripting

Store the last 10 strings. A way to make it elegant?

Hello, Please find an ugly code. bouuu. It shall work onto RAM only to update from the last 10 strings from $URLTO. I shall read first : "$HOME/.fvwmoscfg/fvwmclipboardmplayerplayurl10.ini" The code works but it is very ugly. How could it be made elegant please? Thank you ... (2 Replies)
Discussion started by: french00b
2 Replies

4. Shell Programming and Scripting

Compare strings between 2 arrays and print number in AWK

Hi to everyone, Please some help over here. Hi have array a with 6 elements and array b with 3 elements as shown inside BEGIN{} statement. I need help to get the correct sintax (the part in red) to compare if string from array b is in array a and print the number related for each match.... (3 Replies)
Discussion started by: Ophiuchus
3 Replies

5. UNIX for Dummies Questions & Answers

Strings in arrays

Hi There, Code: Number=10 i=1 while do echo "$i" Check=`find $viewing -name $File_Pattern -type f -Print` i=`expr ${i} + 1` done Unable to store the values in variable 'Check', when i display i am getting Check nothing... (1 Reply)
Discussion started by: Naveen_5960
1 Replies

6. Shell Programming and Scripting

subtraction in bash arrays

hi i am using bash shell to perform some subraction. here is what i have: i have a while loop and am using i as a counter. diff= `expr ${ARRAY1} - ${ARRAY2}` for example array1 has -0.7145 and array2 has -0.7041. when i try the above command, i get expr: non-numeric argument. any... (6 Replies)
Discussion started by: npatwardhan
6 Replies

7. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. Shell Programming and Scripting

arrays in bash

hi guys, i have the following script and when i run it i get blank lines on the screen.. i am trying to display the contents of array var.. #!/usr/bin/bash var=`awk 'NR>20&&NR<31' try.sum | awk '{print $4}'` echo "${var}" (1 Reply)
Discussion started by: npatwardhan
1 Replies

9. Shell Programming and Scripting

Arrays in bash.need help

:confused: Is it possible to delete array elements dynamically.For instance,consider an array( a b c d ) ,now can i delete array (the third element 'c').So that the array becomes array(a b d).. Thanks in advance!! (1 Reply)
Discussion started by: tj23
1 Replies

10. Shell Programming and Scripting

comparing two arrays or strings in bash

Hi there, im having issue with comparing two variables, in a bash script. im trying to do the following: - get a word from user 1 - split the word into array - get a character from user2 trying to compare the character entered by user 2 with every single character in the array... (2 Replies)
Discussion started by: new2Linux
2 Replies
Login or Register to Ask a Question