User Tools

Site Tools


awk:display_line_after_the_matching_string

AWK - Display line after the matching string

awk '/the_string/{getline; print}' file.txt

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/{getline; print}' test.txt

returns:

11   Virginia  Genevive  Roux        45

NOTE: This simply returns the next line after the search string of Peter.


Running

awk '/Roux/{getline; print}' test.txt

returns:

11   Virginia  Genevieve  Roux       45
13   David     Bruce     Stevenson   48

NOTE: This finds the search string Roux in row 10, so it includes the next row 11 in the result; even though row 11 also contains the same search string.

It then proceeds with row 12 which also contains the search string Roux, so it includes row 13 in the results.

No further rows contain the search string so nothing else is added to the result.


Running

awk '/4/{getline; print}' test.txt

returns:

11   Virginia  Genevieve  Roux       45
14   Bob       James     Smith       16
48   Adam      Winter    Ridley      23

NOTE: This finds the search string 4 in row 10, so it includes the next row 11 in the result; even though row 11 also contains the same search string.

It then proceeds from row 12. It does not find the search string 4, so moves to row 13.

In row 13 it finds the search string 4, so it includes the next row 14 in the results; even though row 14 also contains the same search string.

It then proceeds from row 15.

The last row contains the search string; and as there is no other row below that can be added to the result; the last row itself is added. Possibly a bug?


awk/display_line_after_the_matching_string.txt · Last modified: 2021/01/06 17:04 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki