Array in Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array in Shell
# 1  
Old 04-12-2012
Array in Shell

Hi, I have following issue while using Array in Shell script

I have input file as below

Code:
FILE1=filename1,filelocation1
FILE2=filename2,filelocation2
FILE3=filename3,filelocation3
FILE4=filename4,filelocation4
FILE5=filename5,filelocation5
FILE6=filename6,filelocation6

I want traverse these file and get both filename and filelocation in different variables.

Can you please help me?
# 2  
Old 04-13-2012
This is straight forward and should work in both Kshell and bash. After the loop executes two arrays (indexed 0 to n-1) will exist:

Code:
i=0
sed 's/.*=//; s/,/ /' your-file  >/tmp/tfile.$$
while read a b
do
    names[$i]="$a"
    locs[$i]="$b"
    (( i++ ))
done </tmp/tfile.$$
rm /tmp/tfile.$$


The code above can be done in Kshell without using a tmp file; pipe the output from the sed directly into the while. Unfortunately that won't work in bash which is why I presented it the way I did.

This code below is more cryptic, but doesn't require the use of a tmp file to work in bash:
Code:
eval $(awk -F "[=,]" '
    {
        names = names " " $2;
        locs = locs " " $3;
    }
    END { printf( "names=( %s ); locs=( %s )", names, locs ); }
' your-input-file )


Last edited by agama; 04-13-2012 at 12:03 AM.. Reason: wording
This User Gave Thanks to agama For This Post:
# 3  
Old 04-13-2012
And slightly tweaking agama's solution, here's without the use of temp file.

Code:
#! /bin/bash

i=0
while IFS=, read a b
do
    names[$i]=${a#*=}
    locs[$i]=$b
    (( i++ ))
done < inputfile

These 3 Users Gave Thanks to balajesuri For This Post:
# 4  
Old 04-13-2012
Thanks Agama and Balajesuri for your responses...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass C shell array to another C shell script(csh) and shell(sh)

Dear Friends, Please help me on this my script name is send.csh In this i have written the statement like this set args = ( city state country price ) I want to pass this array to another c shell called receiver.csh. and i want to use it in this c shell or how to pass to... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

How to Assign an shell array to awk array?

Hello All, Can you please help me with the below. #!/bin/bash ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5 EXTRACT_DT:30-SEP-12 VER_NUM:1" ARR="No Differences In Stage Between HASH_TOTALS & HASH_TOTALS_COMP For UNINUM:0722075 PROVIDER:5... (14 Replies)
Discussion started by: Ariean
14 Replies

3. Shell Programming and Scripting

Array in shell script

Hi, check=("/usr/local/bin/chk_nag | awk -F":" '{print $1}'" "/usr/local/bin/chk_kas | awk -F":" '{print $1}'" "/usr/local/bin/chk_was | awk -F":" '{print $1}'" ) for i in "${check}"; do echo $i; done when I run this. It says Syntax error: "(" unexpected Please advise. (5 Replies)
Discussion started by: ashokvpp
5 Replies

4. Shell Programming and Scripting

Need help with array in C shell

Hi all, I'm newbie in shell. I'm trying to create files with user name select from database on title with .csh file. So my solution is select name from database into an array, and then foreach name I create a file with that name on title. Like this: set Name = `sqlplus -s ${MYSQL}` <<EOF ... (3 Replies)
Discussion started by: leejung89
3 Replies

5. Shell Programming and Scripting

how to use array variables in shell

Hi, everyone. I wrote a code like this for f in HB021* do program done for f in HB034* do program done for f in HB056* do program done . . (5 Replies)
Discussion started by: xshang
5 Replies

6. Shell Programming and Scripting

Shell string array

Hi all, I want to create an array variable in shell, for example: #!/bin/sh name="foo" name="bar" name="baz" But above code didn't work, I also tried with: name=(foo bar baz) and set -A name foo bar baz but none of these worked. Another question is how to know the shell... (6 Replies)
Discussion started by: Roy987
6 Replies

7. Shell Programming and Scripting

Output of shell in array

Hi, i have a file which reads, arun/manager/200000 pradeep/engineer/10000 karthik/teamlead/30000 ..... i want an output to show, name role salary ======================= arun manager 200000 pradeep engineer 10000 and so on.. i want to do... (9 Replies)
Discussion started by: pradebban
9 Replies

8. Shell Programming and Scripting

Array indexing in shell

Hi , I have 4 array as below Input: servernames=(10.144.0.129 10.144.0.130 10.144.0.131) subfolder_129=(PSTN_SigtranCamel_03 PSTN_SigtranCamel_04 PSTN_SigtranCamel_05) subfolder_130=(SigtranCamel_11 SigtranCamel_12 SigtranCamel_13 SigtranCamel_14 SigtranCamel_15)... (4 Replies)
Discussion started by: sushmab82
4 Replies

9. Shell Programming and Scripting

need help with korn shell array

I have a korn shell script that reads a file with just one column in the file. If the file has more than 5 entries it is split using split -5. This means that is we have 15 entries I will end up with 3 files with 5 entries/lines in each and if I have 23 entries I will end up with 5 files with the... (2 Replies)
Discussion started by: kieranfoley
2 Replies

10. Shell Programming and Scripting

korn shell array?

I read it is possible to provide values for an array with the -A option to the read statement; however, I have not been able to get this to work. When I execute a script with the -A option to the read statement, the shell complains that it is an illegal option. If this works, can someone provide... (5 Replies)
Discussion started by: cstovall
5 Replies
Login or Register to Ask a Question