Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Bash script how to compare arrays elementh to string

The testhomerseklet3.txt contains (in each row) an ‘x’ and ‘y’ coordinates, a date, and a time.
This script is called with 2 arugment, and ‘x’ and a ‘y’ coordinated. I need to write out how many times are there datas at ‘x,y’ coordinates on different days.

howMany=0
declare -a dateT
echo "1 parameter: $1"
echo "2 parameter: $2"
echo
while read line 
do
    x=`echo $line | cut -d "," -f 1`
    y=`echo $line | cut -d "," -f 2` 
    date=`echo $line | cut -d "," -f 3`

    echo $x
    echo $y
    echo $date 
    
    isDateAlready=0
    
    if [ $x = $1 ] && [ $y = $2 ]
    then               
        for dates in "${!dateT[@]}"
        do            
            if [ ${dateT[$dates]} = $date ]
            then                                
                $isDateAlready=1
            fi
        done        
        if [ $isDateAlready -eq 0 ]
        then
            howMany=$(($howMany + 1))
            dateT+=$date
        fi        
    fi    
done <<< $(cat homerseklettest3.txt)

echo "eredmeny: $howMany"

here’s the homerseklettest3.txts content

23.1231234,69.9651548,2000.11.13,7:42,69
69.6969696,11.1111111,1985.8.25,1:1,1
11.2222222,22.3333333,6969.10.1,18:12,23
47.6498634,43.2312457,2120.2.30,14:14,24
92.7418529,99.9999999,1500.10.9,9:20,69
92.7418529,99.9999999,1760.5.10,5:20,21
23.1231234,69.9651548,2010.8.20,16:36,96
92.7418529,99.9999999,1761.5.10,5:20,21
92.7418529,99.9999999,1760.5.10,5:20,21

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  • [ $x = $1 ] && [ $y = $2 ] only works if all the variables are non-empty and don’t contain spaces. Double quote the variables or use [[ ... ]] as I did.
  • When assigning to a variable, don’t use the dollar sign: $isDateAlready=1 is wrong.
  • When adding an element to an array, use parenthses, dateT+=$date just concatenates.
#! /bin/bash
howMany=0
declare -a dateT
echo "1 parameter: $1"
echo "2 parameter: $2"
echo
while read line ; do
    x=`echo $line | cut -d "," -f 1`
    y=`echo $line | cut -d "," -f 2`
    date=`echo $line | cut -d "," -f 3`

    echo x:$x
    echo y:$y
    echo date:$date

    isDateAlready=0

    if [[ $x = $1 && $y = $2 ]] ; then
        for dates in "${!dateT[@]}" ; do
            if [ ${dateT[$dates]} = $date ] ; then
                isDateAlready=1
            fi
        done
        if [ $isDateAlready -eq 0 ] ; then
            howMany=$(($howMany + 1))
            dateT+=($date)
        fi
    fi
done < homerseklettest3.txt

echo "result: $howMany"

But

using an associative array makes the code much simpler:

#! /bin/bash
howMany=0
declare -A dateT
echo "1 parameter: $1"
echo "2 parameter: $2"
echo
while read line ; do
    x=`echo $line | cut -d "," -f 1`
    y=`echo $line | cut -d "," -f 2`
    date=`echo $line | cut -d "," -f 3`

    echo x:$x
    echo y:$y
    echo date:$date

    if [[ $x = $1 && $y = $2 && ! ${dateT[$date]} ]] ; then
        ((++howMany))
        dateT[$date]=1
    fi
done < 1 # homerseklettest3.txt

echo "result: $howMany"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading