User Tools

Site Tools


awk:display_line_before_the_matching_string

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
awk:display_line_before_the_matching_string [2020/09/23 16:11] – created 192.168.1.1awk:display_line_before_the_matching_string [2021/01/06 17:08] (current) peter
Line 4: Line 4:
 awk '/the_string/{print x};{x=$0}' file.txt awk '/the_string/{print x};{x=$0}' file.txt
 </code> </code>
 +
 +<WRAP info>
 +**NOTE:**  This starts at the bottom of the file searching upwards and once it finds the search string it displays everything else above.
 +</WRAP>
 +
 +----
 +
 +====== Example ======
 +
 +Assuming a file, **test.txt**, exists with the following contents:
 +
 +<file>
 +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
 +</file>
 +
 +Running
 +
 +<code bash>
 +awk '/Peter/{print x};{x=$0}' test.txt
 +</code>
 +
 +returns:
 +
 +<code bash>
 +
 +</code>
 +
 +<WRAP info>
 +**NOTE:**  As there is no line about row 10, which contains the search string **Peter**, an empty line is added to the result.
 +</WRAP>
 +
 +
 +----
 +
 +Running
 +
 +<code bash>
 +awk '/Roux/{print x};{x=$0}' test.txt
 +</code>
 +
 +returns:
 +
 +<code bash>
 +
 +10   Peter     Terence   Roux        45
 +11   Virginia  Genevieve  Roux       45
 +</code>
 +
 +<WRAP info>
 +**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.
 +</WRAP>
 +
 +
 +----
 +
 +Running
 +
 +<code bash>
 +awk '/4/{print x};{x=$0}' test.txt
 +</code>
 +
 +returns:
 +
 +<code bash>
 +
 +10   Peter     Terence   Roux        45
 +12   Felix     Devon     Roux         5
 +13   David     Bruce     Stevenson   48
 +14   Bob       James     Smith       16
 +</code>
 +
 +----
awk/display_line_before_the_matching_string.1600877500.txt.gz · Last modified: 2020/09/23 16:11 by 192.168.1.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki