awk '/the_string/{print x};{x=$0}' file.txt
NOTE: This starts at the bottom of the file searching upwards and once it finds the search string it displays everything else above.
Assuming a file, test.txt, exists with the following contents:
10 Peter Terence Roux 45 11 Virginia Genevieve Roux 45 12 Felix Devon Roux 5 13 David Bruce Stevenson 48 14 Bob James Smith 16 48 Adam Winter Ridley 23
Running
awk '/Peter/{print x};{x=$0}' test.txt
returns:
NOTE: As there is no line about row 10, which contains the search string Peter, an empty line is added to the result.
Running
awk '/Roux/{print x};{x=$0}' test.txt
returns:
10 Peter Terence Roux 45 11 Virginia Genevieve Roux 45
NOTE: As there is no line about row 10, which contains the search string Roux, an empty line is added to the result.
Row 11 has the the search string Roux, results in the line above, row 10, being added to the result; even though row 10 also contains the search string.
Row 12 has the the search string Roux, results in the line above, row 11, being added to the result; even though row 10 also contains the search string.
No further rows contain the search string so nothing else is added to the output.
Running
awk '/4/{print x};{x=$0}' test.txt
returns:
10 Peter Terence Roux 45 12 Felix Devon Roux 5 13 David Bruce Stevenson 48 14 Bob James Smith 16