字符串重叠KSH脚本中的数组数据

时间:2013-06-27 20:58:44

标签: arrays string ksh overlapping

我正在尝试从包含作业列表的文件中初始填充数组。然后,我尝试从包含有关这些作业的信息的日志文件中读取。使用此信息,我希望使用提取的数据附加数组。但是我遇到了一个问题。最初我的代码输出应该与" {Job},{Type},{Log},{Process},'来自日志的字符串数据' &#34 ;.然而,在我的代码中发生了一些现在产生输出的东西:" '来自log' og},{Process}和#34;的字符串数据。本质上,它是用提取的字符串数据替换数组的第一部分,而不是在最后添加它。这是代码:

#!/user/bin/ksh93

log="./20130603.log"
list="./working_joblist.dat"
line="./linenum.dat"

# Populate JobArray (TESTING DONE)
i=0
listLines=$(wc -l < $list)

while [[ $i -lt $listLines ]]
do
    JobArray[$i]=$(awk 'NR>=j+2 && NR<j+3 {print $0}' j=$i $list)
    let i=$i+1
done

#READ BEGINS HERE
# Loop through this logic for each index in array
i=0

#server will be a parameter
server="MVS1"

# Grab line number based on the type of server the log came from
linenum=$(awk -F"," '/'"$server"'/{print $2}' $line)

while [[ $i -lt ${#JobArray[@]} ]]
do
    # Assign job to first field in array
    job=$(echo ${JobArray[$i]} | awk -F"," '{print $1}')

    # Initialize startTime if it already exists
    startTime=$(echo ${JobArray[$i]} | awk -F"," '{print $8}')

    # Set up conditional variable       
    state=""        
    if [[ $state = "" ]];then
        # Try to get ended ok data for job
        data=$(awk 'NR>='"$linenum"' && /'"$job"'.*ENDED OK/{
            status = "GREEN,  "
            date = $1",  "
            endTime = $2",  "
            if (startTime == ""){
                # Calculate start time using the ending time and elapsed time
                startTime=endTime-$14",  "
            }
            data=status date startTime endTime
            print data
        }' $log)

        # Assign status data to state. Will determine if match was found
        state=$(echo $data|awk -F"," '{print $1}')

        #if [[ $state = "GREEN" ]];then
            # Remove the job from the working_joblist
            #grep -v "$job" $list > ./truncated
        #fi
    fi  
    if [[ $state = "" ]];then
        # Try to get ended notok data for job
        data=$(awk 'NR>='"$linenum"' && /'"$job"'.*ENDED NOTOK/{
            status = "RED,  "
            date = $1",  "
            endTime = $2",  "
            if (startTime == ""){
                # Calculate start time using the ending time and elapsed time
                startTime=endTime-$14",  "
            }
            data=status date startTime endTime 
            print data
        }' $log)

        # Assign status data to state. Will determine if match was found
        state=$(echo $data|awk -F"," '{print $1}')
    fi
    if [[ $state = "" ]];then
        # Try to get start data for job
        data=$(awk 'NR>='"$linenum"' && /'"$job"'.*STARTED/{
            status = "YELLOW,  "
            date = $1",  "
            startTime = ",  "
            endTime = ",  "
            data=status date startTime endTime
            print data
        }' $log)

        # Assign status data to state. Will determine if match was found
        state=$(echo $data|awk -F"," '{print $1}')
    fi  
    if [[ $state = "" ]];then
        #Try to get waiting data for job
        data=$(awk 'NR>='"$linenum"' && /'"$job"'.*WAITING/{
            status = "BLUE,  "
            date = $1",  "
            startTime = ",  "
            endTime = ",  "
            data=status date startTime endTime
            print data
        }' $log)

        # Assign status data to state. Will determine if match was found
        state=$(echo $data|awk -F"," '{print $1}')
    fi  
    if [[ $state = "" ]];then
        data="WHITE,  ,  ,  ,"
    fi

    # Append data to array
    JobArray[$i]=${JobArray[$i]}$data
    print ${JobArray[$i]}

    # Iterate
    i=$i+1  
done

我尝试过多次修复。我可以将数据变量附加到自身并输出$ data $ data。如果我使用$ {JobArray [$ i]} $ {JobArray [$ i]},JobArray也会自行编写。这让我相信我的阵列有一个方面是关闭的。此外,此打印命令输出的最后一行是正确的,所有行先前都有此错误。

1 个答案:

答案 0 :(得分:0)

似乎问题在于字符串最初是如何存储在第12行的块中的数组中的。以下是我对具有相同问题的人的解决方案:

while [[ $i -lt $listLines ]]
do
    JobArray[$i]=$(awk -F"," 'NR>='"$i"'+2 && NR<'"$i"'+3 {
        print $1",  "$2",  "$3",  "$4",  "
    }' $list)
    let i=$i+1
done