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?
# 8  
Old 04-01-2011
Quote:
Originally Posted by camocazi
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:
Code:
sed -n '/set Runs/{s/.*(//; s/)$//p}' SortScans

What is the format of SortScans?
# 9  
Old 04-01-2011
Quote:
Originally Posted by Chubler_XL
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
---------- 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.
# 10  
Old 04-02-2011
Quote:
Originally Posted by Chubler_XL
It's that spaces on either side of "="
Also you cat get rid of the sub() call my making field seperator "(" or ")"
Thanks Chubler,

I was closeSmilie. My original code, this time a working code is:
Code:
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:
Code:
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!Smilie

Regards
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