15 Practical Grep Command Examples In Linux / UNIX

In this article let us review 15 practical examples of Linux grep command that will be very useful to both newbies and experts.

First create the following demo_file that will be used in the examples below to demonstrate grep command.

$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.

Two lines above this line is empty.
And this is the last line.

1. Search for the given string in a single file

The basic usage of grep command is to search for a specific string in the specified file as shown below.

Syntax:
grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

2. Checking for the given string in multiple files.

Syntax:
grep "string" FILE_PATTERN

This is also a basic usage of grep command. For this example, let us copy the demo_file to demo_file1. The grep output will also include the file name in front of the line that matched the specific pattern as shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files as input to grep.

$ cp demo_file demo_file1

$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.

3. Case insensitive search using grep -i

Syntax:
grep -i "string" FILE


This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.

$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.

4. Match regular expression in files

Syntax:
grep "REGEX" filename


This is a very powerful feature, if you can use use regular expression effectively. In the following example, it searches for all the pattern that starts with “lines” and ends with “empty” with anything in-between. i.e To search “lines[anything in-between]empty” in the demo_file.

$ grep "lines.*empty" demo_file
Two lines above this line is empty.

From documentation of grep: A regular expression may be followed by one of several repetition operators:

  • ? The preceding item is optional and matched at most once.
  • * The preceding item will be matched zero or more times.
  • + The preceding item will be matched one or more times.
  • {n} The preceding item is matched exactly n times.
  • {n,} The preceding item is matched n or more times.
  • {,m} The preceding item is matched at most m times.
  • {n,m} The preceding item is matched at least n times, but not more than m times.

5. Checking for full words, not for sub-strings using grep -w

If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out a normal search will show out all the lines.

The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”.

$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.


The following example is the WORD grep where it is searching only for the word “is”. Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”.

$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

6. Displaying lines before/after/around the match using grep -A, -B and -C

When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match.

Please create the following demo_text file for this example.

$ cat demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

 * e - go to the end of the current word.
 * E - go to the end of the current WORD.
 * b - go to the previous (before) word.
 * B - go to the previous (before) WORD.
 * w - go to the next word.
 * W - go to the next WORD.

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

 * 192.168.1.1 - single WORD
 * 192.168.1.1 - seven words.

6.1 Display N lines after match

-A is the option which prints the specified N lines after the match as shown below.

Syntax:
grep -A <N> "string" FILENAME


The following example prints the matched line, along with the 3 lines after it.

$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

6.2 Display N lines before match

-B is the option which prints the specified N lines before the match.

Syntax:
grep -B <N> "string" FILENAME


When you had option to show the N lines after match, you have the -B option for the opposite.

$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

6.3 Display N lines around match

-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.

$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

7. Highlighting the search using GREP_OPTIONS

As grep prints out lines from the file by the pattern / string you had given, if you wanted it to highlight which part matches the line, then you need to follow the following way.

When you do the following export you will get the highlighting of the matched searches. In the following example, it will highlight all the this when you set the GREP_OPTIONS environment variable as shown below.

$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

8. Searching in all files recursively using grep -r

When you want to search in all the files under the current directory and its sub directory. -r option is the one which you need to use. The following example will look for the string “ramesh” in all the files in the current directory and all it’s subdirectory.

$ grep -r "ramesh" *

9. Invert match using grep -v

You had different options to show the lines matched, to show the lines before match, and to show the lines after match, and to highlight match. So definitely You’d also want the option -v to do invert match.

When you want to display the lines which does not matches the given string/pattern, use the option -v as shown below. This example will display all the lines that did not match the word “go”.

$ grep -v "go" demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

10. Display the lines which does not matches all the given pattern.

Syntax:
grep -v -e "pattern" -e "pattern"
$ cat test-file.txt
a
b
c
d

$ grep -v -e "a" -e "b" -e "c" test-file.txt
d

11. Counting the number of matches using grep -c

When you want to count that how many lines matches the given pattern/string, then use the option -c.

Syntax:
grep -c "pattern" filename
$ grep -c "go" demo_text
6


When you want do find out how many lines matches the pattern

$ grep -c this demo_file
3


When you want do find out how many lines that does not match the pattern

$ grep -v -c this demo_file
4

12. Display only the file names which matches the given pattern using grep -l

If you want the grep to show out only the file names which matched the given pattern, use the -l (lower-case L) option.

When you give multiple files to the grep as input, it displays the names of file which contains the text that matches the pattern, will be very handy when you try to find some notes in your whole directory structure.

$ grep -l this demo_*
demo_file
demo_file1

13. Show only the matched string

By default grep will show the line which matches the given pattern/string, but if you want the grep to show out only the matched string of the pattern then use the -o option.

It might not be that much useful when you give the string straight forward. But it becomes very useful when you give a regex pattern and trying to see what it matches as

$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line

14. Show the position of match in the line

When you want grep to show the position where it matches the pattern in the file, use the following options as

Syntax:
grep -o -b "pattern" file
$ cat temp-file.txt
12345
12345

$ grep -o -b "3" temp-file.txt
2:3
8:3


Note: The output of the grep command above is not the position in the line, it is byte offset of the whole file.

15. Show line number while displaying the output using grep -n

To show the line number of file with the line matched. It does 1-based line numbering for each file. Use -n option to utilize this feature.

$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.

Alternative Raspberry Pi Operating Systems

The original Raspberry Pi has always had a few different operating systems (OSs) available, albeit most of them based on Linux.  With the release of the Raspberry Pi 2 a few more are starting to appear.  The reason behind this is that most Linux operating systems are written to run on the ARMv7 architecture (the CPU at the centre of the Raspberry Pi). The original Pi’s CPU was based on ARMv6. It is therefore becoming much easier for operating systems to be ported to the Pi2.

The OS of choice has always been Raspbian, and there are no plans for it to change.  Raspbian is based on the DebianLinux distribution.  A ‘distribution’ is the word that is often used to describe a flavour of Linux, and probably came about when users used to ‘distribute’ sets of CDs with the operating system and applications on them.  This term has stuck.

Other distributions available for download directly from the Raspberry Pi website are:

  • OpenElec – this is a media centre that takes music, photos and videos served by other devices on your network, streamed channels or files from an attached drive, and allows you to play them back via your monitor or TV.  It works well on the original Pi, and is able to play back 1080p video, but the interface is far more responsive on the Pi2.
  • Pidora – This is another Linux distribution like Raspbian, but is based on the Fedora distribution.  It gives you a different look and feel to Raspbian.  The current build is for the ARMv6 architecture, and therefore will not run on the Pi2.

  • RISC OS – This operating system is different from the others in the fact that it is not based on Linux, but is instead a completely separate OS.  It was originally designed by Acorn in Cambridge and has links to the team that developed the original ARM microprocessors.

  • Snappy Ubuntu Core – With the advent of the ARMv7 in the Raspberry Pi2, a version of the Ubuntu Linux operating system has become available.  This is an early, alpha release, which means that it is not really intended for everyday users, but more for developers to start developing “snappy” apps for Ubuntu.

BUT THERE’S MORE…

With over 5,000,000 Pi’s out there, there’s no surprise that some people are porting their favourite operating system over to the Pi.  The Pi2 has made this much easier because of the additional speed and memory.  So lets look at a few:
  • Android – This is the most popular smartphone operating system in the world and is run (mainly) on ARM based phones – although an Intel based version is now becoming popular with some manufacturers.  I cannot currently find a stable build of the OS for the Pi, but this video shows it in action.  I have no doubt that it will appear some time within the year.
  • Windows 10 – Microsoft announced that their new operating system would run on the Raspberry Pi.  There is little information, and a lot of misinformation about it, like whether it will have a GUI or just be their embedded version (i.e. it will run without a display).  I do not want to say anything that may be wrong, so I’m not going to provide any more information that may mislead people; we will all just have to wait for its release.  What it does say, though, is that Microsoft are supporting machines like the Pi, and when the Compute Module 2 is released (which I am sure will happen) the humble Pi will help them with one of their goals of ‘a Windows PC in every room’.
  • Ubuntu MATE 15.04 – The full Ubuntu MATE distro has been built for the Pi 2 by Ryan Finnie and Sjoerd Simons, providing the whole desktop environment on our little friend.

Ubuntu Mate 15.04

  • Minibian – The default Raspbian image from the Raspberry Pi website contains most of the useful software for those who are starting out with the Raspberry Pi, but some people may not want all that software.  This is where Minibian comes in.  It is VERY small, can fit onto a 512MB SD card and runs on both the Pi and Pi2.  It is really aimed at those wanting to build embedded systems that use the least amount of resources, but it can also be used by those who want to start with a small distribution and add only the software they want to run, like a NAS, web server, or a robot that does not need the GUI and all the other software provided by the full OS.

  • Hypriot – This one is only for the hardy! It’s Raspbian with Docker enabled. What on earth (or the high seas) is that, you ask?  Well, Docker is a way of installing Docker ‘containers’ that contain a number of individual applications and libraries that one would otherwise have to install individually.  This makes it easy to, for example, install a web server with Apache, MySQL and PHP with a pre-determined set of add-ons and configuration; you only need to obtain that ‘container’ and Docker does all the rest.  Head over to Hypriot for more.
  • Arch LinuxArch Linux is another distribution for more experienced users; the base OS is minimal and needs additional packages to be installed by the user to make up the OS into a full environment.  However, it has the reputation of being a good, stable distro.
  • PiPlay – The PiPlay is a pre-built OS for gaming and emulation.  It provides emulation for some of the most popular but old gaming platforms, like the Playstation 1, the Sage Genesis, GeoGeo, SNES, Gameboy and Nintendo Gameboy Advance, Atari 2600, Commodore 64 and others.

This is by no way a full list of operating systems available for the Pi.  There are many, many more, each with its own reason for being.  One I have not mentioned is Volumio, which is based on Raspbian, and serves in a Raspberry Pi/IQaudIO/QAcoustics setup as my HQ media centre.  Others are listed on the eLinux Raspberry Pi area.  Support for some is great, and for others is a little sketchy.  If you can’t find what you are looking for, why not build one of your own?

Understanding IoT: The Internet of Things explained

The Internet of Things (IoT) is not some future concept, nor is it just around the corner; it has been here for some time, and it’s growing. Fueled by the expansion of wireless and cloud computing technology, more things are now connected to the internet than people. That’s all people, not just people on the internet.

What are these “things” which make up the Internet of Things? The IoT is not limited to smartphones and tablets, laptops and desktops. Every year, more and more devices are released capable of internet access, exponentially expanding the universe of internet of things devices.

Heart monitors and insulin pumps generate real-time data available to healthcare professionals caring for patients. Cattle ranchers can monitor cows in the field, not only pinpointing their location, but also identifying those who are pregnant. Power stations, remote pumps feeding oil and gas lines, and even entire assembly lines can now be accessed, monitored and controlled as part of the Internet of Things.

Your car texts you when it needs an oil change or when the tire pressure is getting low. Traffic lights send real-time data on traffic flow, allowing controllers to make adjustments to relieve congestion and helping drivers change their routes. Gas pumps provide price data to consumers, alert distributors about usage and contribute to meta data sales measurements, all information available via the Internet.

The possibilities of the IoT are limited only by the imagination.

With information about the status of everything potentially available to everyone, anywhere, at anytime, security and privacy become serious concerns. A real threat exists, requiring constant vigilance to deal with those concerns.

If you think the IoT hasn’t entered your life yet, look around you. You might be surprised to discover how many devices — from cash registers to parking meters, gas pumps to washing machines — can access and transmit data over the Internet. The question to ask is not when it will effect you; it effects you now. The question is; how will you handle the increased data flow?

A Simple Explanation Of ‘The Internet Of Things’

The “Internet of things” (IoT) is becoming an increasingly growing topic of conversation both in the workplace and outside of it. It’s a concept that not only has the potential to impact how we live but also how we work.  But what exactly is the “Internet of things” and what impact is it going to have on you if any?  There are a lot of complexities around the “Internet of things” but I want to stick to the basics.  Lots of technical and policy related conversations are being had but many people are still just trying to grasp the foundation of what the heck these conversations are about.

Let’s start with understanding a few things.

Broadband Internet is become more widely available, the cost of connecting is decreasing, more devices are being created with wifi capabilities and censors built into them, technology costs are going down, and smart phone penetration is sky-rocketing.  All of these things are creating a “perfect storm” for the IoT.

internet-of-things-2

So what is the Internet of things?

Simply put this is the concept of basically connecting any device with an on and off switch to the Internet (and/or to each other). This includes everything from cell phones, coffee makers, washing machines, headphones, lamps, wearable devices and almost anything else you can think of.  This also applies to components of machines, for example a jet engine of an airplane or the drill of an oil rig.  As I mentioned, if it has an on and off switch then chances are it can be a part of the IoT.  The analyst firm Gartner says that by 2020 there will be over 26 billion connected devices…that’s a lot of connections (some even estimate this number to be much higher, over 100 billion).  The IoT is a giant network of connected “things” (which also includes people).  The relationship will be between people-people, people-things, and things-things.

How does this impact you?

The new rule for the future is going to be, “anything that can be connected, will be connected.”  But why on earth would you want so many connected devices talking to each other?  There are many examples for what this might look like or what the potential value might be.  Say for example you are on your way to a meeting, your car could have access to your calendar and already know the best route to take, if the traffic is heavy your car might send a text to the other party notifying them that you will be late.  What if your alarm clock wakes up you at 6 am and then notifies your coffee maker to start brewing coffee for you? What if your office equipment knew when it was running low on supplies and automatically re-ordered more?  What if the wearable device you used in the workplace could tell you when and where you were most active and productive and shared that information with other devices that you used while working?

On a broader scale the IoT can be applied to things like transportation networks “smart cities” which can help us reduce waste and improve efficiency for things such as energy use; this helping us understand and improve how we work and live.  Take a look at the visual below to see what something like that can look like.

libelium_smart_world_infographic_big

The reality is that the IoT allows for virtually endless opportunities and connections to take place, many of which we can’t even think of or fully understand the impact of today.  It’s not hard to see how and why the IoT is such a hot topic today, it certainly opens the door to a lot of opportunities but also to many challenges.  Security is big issues that is oftentimes brought up.  With billions of devices being connect together what can people to do make sure that their information stays secure?  Will someone be able to hack into your toaster and thereby get access to your entire network?  The IoT also opens up companies all over the world to more security threats.  Then we have the issue of privacy and data sharing.  This is a hot button topic even today so one can only imagine how the conversation and concerns will escalate when we are talking about many billions of devices being connected.  Another issue that many companies specifically are going to be faced with is around the massive amounts data that all of these devices are going to produce.  Companies need to figure out a way to store, track, analyze, and make sense of the vast amounts of data that will be generated.

So what now?

Conversations about the IoT are (and have been for several years) taking place all over the world as we seek to understand how this will impact our lives.  We are also trying to understand what the many opportunities and challenges are going to be as more and more devices start to join the IoT.  For now the best thing that we can do is educate ourselves about what the IoT is and the potential impacts that can be seen on how we work and live.