Following awk
code may help you in same.
awk '
/<\/shsv\/[a-zA-Z]+>/{
val=val?val OFS $0:$0;
print val;
flag="";
next
}
/<[0-9]+>/{
flag=1;
val=$0;
next
}
val && (($0 ~ /<RUN>/) || ($0 ~ /<AL_Ptime>/) || ($0 ~ /<lsf_login07>/)){
val=val?val OFS $0:$0
}
' Input_file
Output will be as follows.
<594711> <RUN> <AL_Ptime> <lsf_login07> </shsv/DASA>
<594712> <RUN> <AL_Ptime> <lsf_login07> </shsv/CXZC>
EDIT: Adding explanation for code here too.
awk '
/<\/shsv\/[a-zA-Z]+>/{ ##Search for string shsv with alphabets till their group in a line, then do following.
val=val?val OFS $0:$0; ##create variable named val whose value is current line when val is NULL else it will concatenating its own value with each line.
print val; ##printing variable val here.
flag=""; ##making variable flag as NULL here.
next ##Using next will skip all statements further.
}
/<[0-9]+>/{ ##Search <digits till group and if it is TRUE then do following.
flag=1; ##Setting variable flag to 1 now.
val=$0; ##making variable val as current line.
next ##Using next will skip all statements further.
}
val && (($0 ~ /<RUN>/) || ($0 ~ /<AL_Ptime>/) || ($0 ~ /<lsf_login07>/)){ ##Checking if variable val is NOT NULL AND (either current line is string <RUN> OR <AL_Ptime> OR <lsf_login07>) then do following.
val=val?val OFS $0:$0 ##create variable named val whose value is value of current line if va; is NULL else it will concatenate its own value with current line.
}
' Input_file ##Mentioning the Input_file name here.