С awk
:
awk '/Pattern2/ {line=$0; next}; /First Pattern/ {print line; exit}' file.txt
/ Pattern2 / {line = $ 0; next}
: если шаблон Pattern2
сопоставлен, сохранение строки в переменной line
и переход к следующей строке
/ First Pattern / {print line; exit}
: если найден Первый шаблон
, печать переменной строка
и выход из
Пример:
% cat file.txt
...
Pattern2:TheWrongBar
foo
Pattern2:TheRightBar
foo
First Pattern
foo
...
% awk '/Pattern2/ {line=$0; next}; /First Pattern/ {print line; exit}' file.txt
Pattern2:TheRightBar