I need to create a script that does the following:
Read the file for the occurrences of "EXECUTE" and "END" strings.
There will be several occurrences of EXECUTE and END strings on the file.
The resulting lines in #1, needs to be searched for the word "Error|failed|error|warning", if the word error occurs on the line, prints the specific line and 1 line above it.
The essential point is that pline captures the previous line when it wasn't printed; and start is true when you have seen an EXECUTE but not yet an END. So when start is true and the regular expression matches, you print the previous line and the current line.
The dot matches lines which contain a character. If you want a non-blank character, change the dot to something like [^ ]
The awk script handles the whole file in one go; by the time your shell regains control over execution, "pline" will no longer be meaningful.
Generally, to get both output on standard output and a value into a variable from a single child process is extremely tricky. My suggestion would be to make the awk script print its output in such a form that you can then unambiguously identify which lines are "plines" when reading the output back into the shell script. Something like
awk '... print "pline " pline; print "output " $0 ' ... |
while read type line; do
case $type in
pline) echo this is a pline: "$line";;
output) echo this is real output for that pline: "$line";;
esac
done
Do you see what I'm getting at? Make awk print a single extra identifier at the start of every line, so your shell script can read that and discard it, but then handle the different types of lines differently depending on what the value of the discarded identifier was.