I have a text like:
set 1 recovery 2 2
set 2 recovery 2 2
% ERROR Unknown error
set 3 recovery 2 2
set 4 recovery 2 2
% ERROR Unknown error
What I need is an output like:
set 1 recovery 2 2
set 2 recovery 2 2 ==> % ERROR Unknown error
set 3 recovery 2 2
set 4 recovery 2 2 ==> % ERROR Unknown error
I tried with awk and sed, but I'm failing. What can I try next?
Assumptions:
ERROR
line before each ERROR
lineERROR
linesSample data:
$ cat check.dat
set 1 recovery 2 2
set 2 recovery 2 2
% ERROR Unknown error
set 3 recovery 2 2
set 4 recovery 2 2
% ERROR Unknown error
One awk
solution where we rely on printf
to leave us on the current line until we explicitly tell it to print a newline (\n
):
awk '
/ERROR/ { printf " ==> %s",$0 ; next }
{ printf "%s%s",nl,$0 ; nl="\n" }
END { printf "\n" }
' check.dat
Where:
/ERROR/ ...
- if current line includes the string ERROR
then append ==>
and the current line to the output; then skip rest of processing and go to the next line of input{ printf "%s%s",nl,$0 ; nl="\n" }
- for lines w/out ERROR
we print the line but first we print the nl
variable; initially nl=""
so for the first line of input we'll print to the first line of output; for successive non-ERROR
lines we want nl="\n"
so we can move the cursor off the end of the previous line and down to a new line before we print our non-ERROR
line of textEND ...
- print one last new line characters (\n
)Results of running the above:
set 1 recovery 2 2
set 2 recovery 2 2 ==> % ERROR Unknown error
set 3 recovery 2 2
set 4 recovery 2 2 ==> % ERROR Unknown error