Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

This can be achieved using the grep command with the -B option. The -B option allows you to specify the number of lines to display before the matched line.

For example, if you want to output a line that has the pattern "success" only if it is preceded by a line that has the pattern "login", you can use the following command:

grep -B 1 "success" file.txt | grep "login"

This command will search for the pattern "success" in the file.txt and display the line that matches, along with the line that precedes it (specified by the -B 1 option). The output is then piped to another grep command that searches for the pattern "login". This will ensure that the line with the pattern "success" is only displayed if it is immediately preceded by a line that has the pattern "login".