1

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

'''

4
  • I can't see the input of sed. Commented Jun 16, 2020 at 16:24
  • Do you have an example file to demonstrate the problem? A small example would be fine. If you could also give an example of the expected output that'd be really good. Commented Jun 16, 2020 at 16:25
  • The output file is about the same as the output. I'm taking any line that has a date and just writing it back out to the new file. I then look to see if there isn't a date on the line where "Status" is, such as the line that has Driver: or Channel: I then grab the date of the prior line (hopes to grab) and write it out so that there is a timestamp on the Status line.So the original file has the Status line similar to the channel and driver line with no date / timestamp. I'm trying to get a file where the status line always has the date Commented Jun 16, 2020 at 16:35
  • Code blocks are delimited by triple backticks, ```, not single quotes, ''' – you still get code formatting because you also indent your code blocks. You don't need to do both. Commented Jun 16, 2020 at 17:30

1 Answer 1

1

It seems like your sed command does not have an input passed to it - meaning it will grab all the remaining output coming from < $input, making it consume all the remaining lines at once.

It seems you wanted to do this:

linefix=$(echo "$line" | sed -n -e 's/^.*\(Status: \)/\1/p')

Or, if you'd like to be fancier and save on an echo and a pipe, you could use a herestring:

linefix=$(sed -n -e 's/^.*\(Status: \)/\1/p' <<< "$line")
Sign up to request clarification or add additional context in comments.

1 Comment

You could also use bash's built-in substitution to do the job: linefix="${line/#*Status: /Status: }"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.