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?
Prev   Next
# 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.
 
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