2

全部

我有两个二维数组文件要用 bash 读取

文件 1.txt (nx6)

DESC1 DM1 W1 S1 CRITICAL1 GRADE1 #this line dosen't exist
EEE 1x 9 6 Y E2 
AAA X 1 2 Y E2 
C+C X 8 1 Y T1 
HHH 1x 5 5 Y T2 
III X 5 5 Y S1 
JJJ X 5 5 Y S1 
D+ X 2 3 Y T1 

文件 2.txt (mx3)

DESC2 W2 S2 #this line dosen't exist
AAA 1 2 
EEE 9 5 
FFF 10 7 
GGG 4 9 
III 5 5 
C+C 8 1 
D+ 2 3 
JJJ 5 5 

我想做的是提取两个文件中的元素,然后进行一些比较,如下图:

两个木偶 http://imgur.com/4479luK.jpg

我真正想做的绿色标签是在 $DESC1 中获取一个元素并与 ${DESC2[@]} 中的整个元素进行比较,如果在 ${DESC2[@]} 中找到/没有找到元素,则反馈为真/错误的

这是我的工作:

#!/bin/bash

clear

ESC=`printf "\033"`

##===================================================================##
##===================================================================##
##========== read information from file1.txt and file1.txt ==========##
##===================================================================##
##===================================================================##

idx1=0

while read -a file1array$idx1; do
    let idx1++
done < file1.txt

idx2=0

while read -a file2array$idx2; do
    let idx2++
done < file2.txt

##===================================================================##
##===================================================================##
##================ start to compare these two files =================##
##===================================================================##
##===================================================================##

for ((i=0; i<idx1; i++)); do
   for ((j=0; j<idx2; j++)); do

        DESC1="file1array$i[0]"
        DM1="file1array$i[1]"
        W1="file1array$i[2]"
        S1="file1array$i[3]"
        CRITICAL1="file1array$i[4]"
        GRADE1="file1array$i[5]"

        DESC2="file2array$j[0]"
        W2="file2array$j[1]"
        S2="file2array$j[2]"

        [ $i == 0 ] && LOSSarray+=(["$j"]="${!DESC2}")

        if [[ "${!GRADE1}" == [E-GT-Z][1-9] && "${!DESC1}" == "${!DESC2}" ]]; then

             W1_Judge=`expr "scale=3; ${!W1} - ${!W2}" | bc`
             S1_Judge=`expr "scale=3; ${!S1} - ${!S2}" | bc`

             [ $W1_Judge != 0 -o $S1_Judge != 0 ] && declare -A jgWS=( ["${!DESC1}"]="WSNG" )

        elif [[ "${!GRADE1}" == [E-GT-Z][1-9] && "$j" == `expr $idx2 - 1` ]]; then

             [[ -z $(echo "${LOSSarray[@]}" | grep -o "${!DESC1}") && "${!CRITICAL1}" != "NULL" ]] && declare -A jgLOSS=( ["${!DESC1}"]="LOSSNG" )

        elif [[ "${!GRADE1}" != [E-GT-Z][1-9] && "${!DESC1}" == "${!DESC2}" ]]; then

             [[ "${!DM1}" == [1-2]x* || "${!DM1}" == "X" ]] && declare -A jgEXTRA=( ["${!DESC1}"]="EXTRANG" )

        fi

    done


    if [[ "${jgWS[${!DESC1}]}" == "WSNG" ]]; then

    echo "${!DESC1}: ${ESC}[0;31mWidth or Space NG${ESC}[0m"

    elif [[ "${jgLOSS[${!DESC1}]}" == "LOSSNG" ]]; then

    echo "${!DESC1}: ${ESC}[0;31mLOSS NG${ESC}[0m"

    elif [[ "${jgEXTRA[${!DESC1}]}" == "EXTRANG" ]]; then

    echo "${!DESC1}: ${ESC}[0;31mEXTRA NG${ESC}[0m"

    else

    echo "${!DESC1}: OK"

    fi


done

可以继续整个脚本并输出以下结果:

EEE: Width or Space NG
AAA: OK
C+C: OK
HHH: LOSS NG
III: EXTRA NG
JJJ: EXTRA NG
D+: OK

但是如果我像这样更改file1.txt(将带有“D +”的行改为line1):

D+ X 2 3 Y T1 
EEE 1x 9 6 Y E2 
AAA X 1 2 Y E2 
C+C X 8 1 Y T1 
HHH 1x 5 5 Y T2 
III X 5 5 Y S1 
JJJ X 5 5 Y S1 

我得到以下结果:

D+: syntax error: operand expected (error token is "+")
  1. 如果我不想编辑 file1.txt,我该如何解决这个问题?

  2. 如何将 file2.txt 的第一列读取为不需要在 for 循环中执行的数组?

4

1 回答 1

3

问题是,如果您尝试使用+. 例如:

$ echo "${arr[D+]}"
-bash: D+: syntax error: operand expected (error token is "+")

$ echo "${arr[foo]}" # works

但是,如果您先声明数组,它会起作用:

$ declare -A arr
$ echo ${arr[D+]} # works

因此,在您在 if 语句中访问它们之前,您需要确保预先声明了数组jgWS,jgLOSS和。jgEXTRA

例如,当前jgWS仅当满足此条件时才声明数组:[ $W1_Judge != 0 -o $S1_Judge != 0 ]. 稍后,您尝试在此处使用数组:if [[ "${jgWS[${!DESC1}]}" == "WSNG" ]],但它可能尚未初始化,在这种情况下它会失败。

于 2014-03-24T10:50:32.963 回答