How To Print Matching Line and All After Using awk

Published Date Author: , Posted August 1st, 2019 at 4:53:41pm

Every time I use awk, my respect for it grows! Like grep, find and rsync, it is one of the powerhouse command-line tools in my arsenal.

Recently, I needed to list all MySQL binary log files from a starting file forwards in sequence. AWK to the rescue!

For example, I knew the starting file was mysql-bin.000325, and also that there is a list of binary log files kept by MySQL called mysql-bin.index, so I executed the following to extract the lines I wanted out of the 76 available in the file:

Per the man page:
“Two patterns separated by a comma is called a range pattern. With it, once the first pattern is matched, every subsequent record matches until the second pattern is matched.”

We can call the first argument the “starting match pattern” and the second argument the “ending match pattern“.

Awk will start to print lines (the default behavior for a match) when it finds the starting match pattern and end after it finds and prints the ending match pattern line.

Since the ending match pattern is zero, it will never be true and so awk simply prints all remaining lines in the source.

No comments as yet.

Leave Your Comment  Leave a comment

All fields marked with "*" are required.