简体   繁体   English

遍历浮动数组以便在bash中进行比较

[英]Traversing floating array for comparison in bash

I am extracting float column from csv file and storing in row_arr array. 我从csv文件中提取float列并存储在row_arr数组中。 Next am comparing a stored value in sub_val against the array elements. 接下来我将sub_val中的存储值与数组元素进行比较。 I want to store index of the matching element in a variable. 我想在变量中存储匹配元素的索引。 I have the following code and errors. 我有以下代码和错误。

declare -a fileList=(
"file-a"
"file-b"
"file-c"
)

loc='../myData/'

file_name="sample.csv"
png='.png'
sep=', '
txt='.txt'

fileList_size="$(echo ${#fileList[@]})"

row_no=1
counter=0
counter2=0

for j in "${fileList[@]}"
do
        echo "Sub Row No = "$row_no
        #Picking value from 2nd column for other sample file
        sub_val=($(awk -F, 'FNR = "'"$row_no"'" {print $2}' $file_name))

        #### Storing Time Sequence in Array from Other File
        while IFS=, read -a csv_line;
        do 
            row_arr[$rc]=${csv_line[0]};
            #echo "This is row array"
            echo ${row_arr[$rc]}
            ((rc++))
        done < $loc${j}$txt

        row_arr_size="$(echo ${#row_arr[@]})"

        for ((i = 0; i < ${#row_arr[@]}; ++i));
        do

            if [ $(echo "${row_arr[$i]} >= $sub_val" | bc -l) -eq 1 ]
            then
                    echo ${row_arr[$i]} ">=" $sub_val
                    p='p'
                    pos=$i
                    echo "Index is="$pos

                    got_row=$pos
                    got_value="$(echo `sed -n $pos$p $loc${j}$txt`)"

                    echo "File="$loc${j}
                    echo "Row No="$got_row
                    echo "Value="$got_value
                    echo "Counter="$counter2
                    echo "##################################################################"

                    break

            fi
        done

                counter2=$[$counter2+1]
                if [ $counter2 -eq $fileList_size ]
                then
                    echo $counter2
                    exit 1
                fi
        row_no=$[$row_no+1]
        counter=$[$counter+1]
done

and i get this error: 我收到此错误:

[: -eq: unary operator expected

if i replace >= with -ge then i get this error: 如果我用-ge替换>=然后我得到这个错误:

integer expression expected

Following command gives the same error 以下命令给出了相同的错误

[  -eq 1 ]

it can occur if bc output is empty, which occurs in case of error 如果bc输出为空,则会发生这种情况,如果出现错误则会发生

a simple workaround is to handle the empty output like 一个简单的解决方法是处理空输出,如

[[ $(bc -l <<<"${row_arr[$i]} >= $sub_val") != 1 ]]
  • The double bracket is parsed differently if the first expansion is empty, first argument is empty string 如果第一个扩展为空,则第一个参数为空字符串,则双括号的解析方式不同
  • != is a string comparison, handles the empty string whereas -eq requires an integer, because bc output can be 0 1 or !=是字符串比较,处理空字符串,而-eq需要一个整数,因为bc输出可以是0 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM