1. Overview
In this tutorial, let’s look at how we can use grep to get information around a matched string.
2. Sample Text File
The grep filter searches a file for a particular pattern of characters and displays all lines that contain that pattern. We’ll use the grep command to search a string in a text file and then print the surrounding lines. Let’s create a file by redirecting the output of the ip addr show command to a file (ipinfo.txt):
$ ip addr show > ipinfo.txt
$ cat ipinfo.txt
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:ea:a5:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.104/24 brd 192.168.0.255 scope global dynamic noprefixroute enp0s3
valid_lft 6826sec preferred_lft 6826sec
inet6 fe80::129e:c85e:f067:e115/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3. Displaying Lines Before a Match
While the grep command offers many options, some of the most interesting flags are A, B, and C. These options allow us to get the context around a matched string. The -A flag represents after and prints a specified number of lines after a matched string. The -B flag stands for before, and it prints the lines before a match. Lastly, we’ve got option -C which prints the lines both before and after the matched string.
Let’s run grep with the -B option:
$ grep '/24' -B2 ipinfo.txt
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:ea:a5:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.104/24 brd 192.168.0.255 scope global dynamic noprefixroute enp0s3
When grep finds a match, it returns the surrounding set of lines that we’ve specified.
4. Displaying Lines After a Match
We use the -A flag to display lines after a match and specify the number of lines we need after that match:
$ grep '/24' -A2 ipinfo.txt
inet 192.168.0.104/24 brd 192.168.0.255 scope global dynamic noprefixroute enp0s3
valid_lft 6826sec preferred_lft 6826sec
inet6 fe80::129e:c85e:f067:e115/64 scope link noprefixroute
We can even combine these commands to get a more effective output:
$ grep '/24' -A2 -B2 ipinfo.txt
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:ea:a5:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.104/24 brd 192.168.0.255 scope global dynamic noprefixroute enp0s3
valid_lft 6826sec preferred_lft 6826sec
inet6 fe80::129e:c85e:f067:e115/64 scope link noprefixroute
5. Using the -C Option
Finally, we can use -C to print lines before and after a match. Let’s use it to achieve the same result as the previous example:
$ grep '/24' -C2 ipinfo.txt
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:ea:a5:61 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.104/24 brd 192.168.0.255 scope global dynamic noprefixroute enp0s3
valid_lft 6826sec preferred_lft 6826sec
inet6 fe80::129e:c85e:f067:e115/64 scope link noprefixroute
6. Conclusion
In this tutorial, we saw how the grep command makes our work easy, as we can filter and specify the number of lines around the match. We’ve discussed how to specify the number of lines before and after a match. We mainly use the A, B, and C options to get the lines around a match, and we can also use any or all of them in a single search.