One thing I would say, is to do it all in one process rather than having one nawk piped into another nawk process. Depending upon how many lines of input this code needs to go through you'd could have a substantial savings in processing time.
This works for me (I don't have nawk but it should work for you):
#!/usr/bin/env bash
# @(#) s1 Demonstrate extract +-1, combine,
# Section 1, setup, pre-solution.
# Infrastructure details, environment, commands for forum posts.
# Uncomment export command to run script as external user.
# export PATH="/usr/local/bin:/usr/bin:/bin"
set +o nounset
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe ; pe "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
pe "(Versions displayed with local utility \"version\")"
c=$( ps | grep $$ | awk '{print $NF}' )
version >/dev/null 2>&1 && s=$(_eat $0 $1) || s=""
[ "$c" = "$s" ] && p="$s" || p="$c"
version >/dev/null 2>&1 && version "=o" $p printf specimen grep paste sed
set -o nounset
pe
FILE=${1-data1}
# Display sample of data file, with head & tail as a last resort.
pe " || start [ first:middle:last ]"
specimen $FILE \
|| { pe "(head/tail)"; head -n 5 $FILE; pe " ||"; tail -n 5 $FILE; }
pe " || end"
# Section 2, solution.
pl " Results:"
grep -A1 -B1 'threshold' $FILE |
tee f1 |
grep -v '^--$' |
tee f2 |
paste -d" " - - - |
tee f3 |
sed 's/$/\n/' |
tee t1
# Section 3, post-solution, check results, clean-up, etc.
n1=$(wc -l <expected-output.txt)
n2=$(wc -l < t1)
pl " Comparison of $n2 created lines with $n1 lines of desired results:"
if [ ! -f expected-output.txt -o ! -s expected-output.txt ]
then
pe " Comparison file \"expected-output.txt\" zero-length or missing."
exit
fi
if cmp expected-output.txt t1
then
pe " Passed -- files have same content."
else
pe " Failed -- files not identical -- detailed comparison follows."
if diff -B -b expected-output.txt t1
then
pe " Passed by ignoring whitespace differences."
fi
fi
exit 0
producing:
% ./s1
Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution : Debian GNU/Linux 5.0.8 (lenny)
GNU bash 3.2.39
printf - is a shell builtin [bash]
specimen (local) 1.17
GNU grep 2.5.3
paste (GNU coreutils) 6.10
GNU sed version 4.1.5
|| start [ first:middle:last ]
Edges: 5:0:5 of 32 lines in file "data1"
ABCD DEFG5 42.0.1-63.38.31
KKKK iokl IP Connection Available
---
ABCD DEFG5 42.0.1-63.38.31
SLK A pcnbod RCVRY-LINK-CGST: threshold has cleared
SC=01 LLI=pcnbod CLASS=VL
|| end
-----
Results:
ABCD DEFG5 42.0.1-63.38.31 SLK A FFFFFF REPT-LINK-CGST: threshold level 0 to 1 SC=00 LLI=mmmmm CLASS=SL
ABCD DEFG5 42.0.1-63.38.31 SLK A FFFFFF RCVRY-LINK-CGST: threshold has cleared SC=00 LLI=mmmmm CLASS=SL
ABCD DEFG5 42.0.1-63.38.31 SLK A pcnbod REPT-LINK-CGST: threshold level 0 to 1 SC=01 LLI=pcnbod CLASS=VL
ABCD DEFG5 42.0.1-63.38.31 SLK A pcnbod RCVRY-LINK-CGST: threshold has cleared SC=01 LLI=pcnbod CLASS=VL
-----
Comparison of 8 created lines with 7 lines of desired results:
cmp: EOF on expected-output.txt
Failed -- files not identical -- detailed comparison follows.
Passed by ignoring whitespace differences.
See temporary files f? for intermediate results, man pages for other details ... cheers, drl