问题描述:

I am trying to initially populate an array from a file with a list of jobs. I then attempt to read from a log file that has information about those jobs. Using this information I wish to append the array with the extracted data. However I have run into an issue. Initially my code output as it should with "{Job}, {Type}, {Log}, {Process}, 'String data from log' ". However something has happened in my code that now makes the output: " 'String data from log'og}, {Process},". Essentially it is replacing the first portion of the array with the extracted string data instead of tacking it on at the end. Here is the code:

#!/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

I've tried to do multiple fixes. I can append the data variable to itself and output $data$data. Also the JobArray will write over itself if I use ${JobArray[$i]}${JobArray[$i]}. This leads me to believe that there is an aspect of my Array that is off. Also, the very last line that is output by this print command is correct, all lines prior have this error.

网友答案:

It seems that the problem was with how the strings were initially stored in the array in the block at line 12. Below is my solution for those who have the same problem:

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
相关阅读:
Top