My file seems to be getting written out the linefix variable over and over after evaluating the sed Status: code.
any ideas on what i did wrong with the nesting? Or is it the variable "prior" that is being set prior to the while, and then used in the nested if statement? Setting it to string Null as we don't have a prior line initially, then every time it finds a date, I write out a prior line, in hopes that when I don't find a date on a line, I can append it to the line with a status value.
code: '''
input="/var/log/alertError.log"
prior="NULL"
while IFS= read -r line ; do
if [[ $line =~ [0-9]{2}/[0-9]{2}/[0-9]{2} ]]; then
echo "$line" >> /var/log/alertErrorFinal.log
prior=$line
else
if [[ "$line" =~ 'Status:' ]]; then
linefix=$(sed -n -e 's/^.*\(Status: \)/\1/p')
priordate=$(echo "$prior" | awk -F'[][]' '{print $2}')
echo "$priordate $linefix" >> /var/log/alertErrorFinal.log
else
echo "$line" >> /var/log/alertErrorFinal.log
fi
fi
done < $input
'''
Output:
'''
jcl/jclnt.log-[05/05/20 02:20:15.669]:jclnt PT:Filtering out notification-only attributes.
jcl/jclnt.log-[05/05/20 02:20:15.669]:jclnt PT:Pumping XDS to ldap.
jcl/jclnt.log-[05/05/20 02:20:15.669]:jclnt PT:Performing operation status for .
jcl/jclnt.log-[05/05/20 02:20:15.669]:jclnt PT:
jcl/jclnt.log-DirXML Log Event -------------------
jcl/jclnt.log- Driver: \StarWars\system\Driver Set\AD-wookie
jcl/jclnt.log- Channel: Publisher
05/05/20 02:20:15.669 Status: Error
Status: Error
Status: Error
Status: Error
Status: Error
'''
Input: '''
jcl/jclnt.log-[05/05/20 02:20:15.669]:jclnt PT:Filtering out notification-only attributes.
jcl/jclnt.log-[05/05/20 02:20:15.669]:jclnt PT:Pumping XDS to ldap.
jcl/jclnt.log-[05/05/20 02:20:15.669]:jclnt PT:Performing operation status for .
jcl/jclnt.log-[05/05/20 02:20:15.669]:jclnt PT:
jcl/jclnt.log-DirXML Log Event -------------------
jcl/jclnt.log- Driver: \StarWars\system\Driver Set\AD-wookie
jcl/jclnt.log- Channel: Publisher
jcl/jclnt.log- Status: Error
'''
sed.```, not single quotes,'''– you still get code formatting because you also indent your code blocks. You don't need to do both.