So basically what I want to do is use a grep
,sed
or awk
command (I'm not picky) that can do the following. I know how to go from Vuln 1
to OS1
but not vice versa.
Input:
test.txt
Vuln 1
OS1
OS2
OS3
Vuln 2
OS4
OS5
Output (heavy pseudocode):
$ grep "OS1" test.txt
Vuln 1
I guess, this is what you need:
Explanation:
perl -ne
reads the input linewisechomp
removes newline at the end (not necessary, but I think, it is cleaner)$h
contains the content of the line, if it starts withV
$h
is printed if the current line containsOS1
In this solution we use GNU AWK.
Explanation
RS=""
)-F"\n"
)/OS1/ {print $1}
)You can do:
We are using PCRE (
-P
) and ASCII NUL as input line delimeter (-z
) instead of\n
so that we can match\n
literally\n?\K
will match an optional newline before our desiredVuln 1
,\K
will discard the match(?=\n\s*OS1\n)
is the zero width positive lookahead pattern ensuring thatVuln 1
is followed byOS1
in a\n
separated line.Example: