Array comparision in bash shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array comparision in bash shell
# 1  
Old 08-22-2012
Array comparision in bash shell

I'm not sure if i can put the problem in understandable form.Let me try:


I have a array which is like and always fixed:
Array1=(new inprogress pending Restored Resolved )

Other 2 array array2 which may varry but always subset of above array:
Like it can be :
Array2=(New inprogress Resolved)
Array 3 always corresponds to array2. Which says time spent in each state if array2.Like
Array3=(2min 3 min 4min)

Now, i have to display the time spent for each state of array2 in the order they are in array1 and if any element of array1 doesnt exist in array2 implies its 0 min.

So in above example output should be Like:
new => 2min
inprogress =>3 min
pending =>0 min
Restored =>0min
Resolved => 4min

Could u pls help me in that ?
# 2  
Old 08-22-2012
In case elements of Array2 are identical to the ones of Array1 (which they are not, "new" - "New") and count of elements in Array2 is equal to the count of Array3 (which it is not, 3 - 4 due to space in "3 min") this might do the job:
Code:
for (( i=0; i<${#Array1[@]}; i++ ))
        do echo -n ${Array1[$i]}" => "
           for (( j=0; j<${#Array2[@]}; j++ ))
                do [ ${Array1[$i]} == ${Array2[$j]} ] && { tmp=${Array3[$j]}; break; } || tmp="0min"
                done 
           echo $tmp
        done

May be refined.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 08-24-2012
Thanks a lot it was very much helpful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

2. Shell Programming and Scripting

Bash shell script undefined array item value question

Hello, I'm new here. I test these expressions's value in my script : (in centOS 6 ) #!/bin/bash array='something' echo "############" echo ${array} echo ${array} echo ${array} echo "############" The output result is : ################# something something #################... (5 Replies)
Discussion started by: lingjing
5 Replies

3. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

4. UNIX for Dummies Questions & Answers

Bash Floating Number Variables Comparision

I am trying to compare the floating number variables but i am receiving an error, can you please help what is wrong. Thank you. #!/bin/bash var1=100.25 var2=100.25 if (( $var1 == $var2 )); then echo "Matching" else echo "Not Matching" fi Error: ./number.sh: line... (6 Replies)
Discussion started by: Ariean
6 Replies

5. UNIX and Linux Applications

Unix Shell Scripting : Comparision of two files

Hi, We need to compare a text file File1.txt and config file File2.txt in a way that it checks if the content of File1.txt exists between the range mentioned in File2.cfg. The range here is the range between col1 and col2 of File2.cfg If the content of File1.txt lies between the range of... (12 Replies)
Discussion started by: CFA
12 Replies

6. Shell Programming and Scripting

Shell snip to import CSV data into BASH array

I have been trying to write a simple snip of bash shell code to import from 1 to 100 records into a BASH array. I have a CSV file that is structured like: record1,item1,item2,item3,item4,etc.,etc. .... (<= 100 items) record2,item1,item2,item3,item4,etc.,etc. .... (<= 100 items)... (5 Replies)
Discussion started by: dstrout
5 Replies

7. UNIX for Dummies Questions & Answers

File comparision and modification using shell script

Hello everyone, I would like to know how to compare two files and modify any differences with some other data using shell script. I think it would be better understood with an example. I got two files named 'filex' and filey'. 'filex' is constant file without any changes in data. 'filey' is... (2 Replies)
Discussion started by: maddy81
2 Replies

8. Shell Programming and Scripting

error while doing decimal comparision in shell

a=10 b=10.6 c=$(echo "$a - $b" | bc) if ] echo "success" else echo "failure" fi while executing the above sample code, am getting the below error: seems unix is comparing .6 with 0 instead of 0.6 with 0. can anyone help me in solving this ? regards, (7 Replies)
Discussion started by: cmaroju
7 Replies

9. Shell Programming and Scripting

String comparision in shell scripting

Hi Guys, I am new to scripting I have written a code to compare strings,but I am getting some Exception Code snippet: MODE="D" if ]; then . $file1 fi Error: ./BatchJobs.sh: [[: execute permission denied I have given all Execute permissions to the script(chmod 755... (2 Replies)
Discussion started by: Anji
2 Replies

10. Shell Programming and Scripting

File Comparision by using Shell Script

Hello All, I am trying to find 2 file comparision by using Shell Script. For example, I am having 2 directories namely DAY1 & DAY2. DAY1 directory contains file1.dat, file2.dat, file3.dat, file4.dat, file5.dat & DAY2 directory contains file1.dat, file2.dat, file3.dat, file4.dat, file5.dat. Now,... (3 Replies)
Discussion started by: nvkuriseti
3 Replies
Login or Register to Ask a Question