User Tools

Site Tools


awk:display_line_before_the_matching_string

This is an old revision of the document!


AWK - Display line before the matching string

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.


Example

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:

 

Running

awk '/Roux/{print x};{x=$0}' test.txt

returns:

10   Peter     Terence   Roux        45
11   Virginia  Genevieve  Roux       45

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

awk/display_line_before_the_matching_string.1609950954.txt.gz · Last modified: 2021/01/06 16:35 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki