Grep in Shell Script: the Basic you Need to Know
Here you will find out:
- about grep command in Linux shell script
- how to use it
- when DiskInternals can help you
Are you ready? Let's read!
About grep in Linux shell script
Often, programmers need to find a file that contains a specific line or a specific word in that line. On Linux, this is accessible with one exact, simple but powerful grep command - grep stands for "global regular expression print". It can also find strings by pattern or regular expressions, and also filter the output of commands, and much more.
All these are done with instant speed, so it is very convenient to use.
Examples of shell grep commands
All commands are not complex and lend themselves to simple logic. Here are the most important and basic commands for starting a string search.
grep -i 'bar' filename1 - search for the word 'bar' in Linux (case insensitive)
grep 'word' filename2 - search in Linux on any line containing the word “word” in the file name
grep -c 'nixcraft' frontpage.md - search and count the total number of times the line 'nixcraft' appears in a file called frontpage.md
grep -R 'foo' - look for the word “foo” in all files in the directory and in all its subdirectories in Linux
Examples of grep's syntax in Linux
Examples of command syntax are as follows:
These are various commands for finding strings, regular expressions in multiple files, and even in a directory, using recursive mode.
Practical use of grep command
Now, it’s time from theory to approach practice.
- 1. We start by looking for user boo in the Linux password file. The command looks like this:
The result will look something similar:
You can be case insensitive (whether capital and lowercase letters) for the program will be the same (e.g. ABC, Abc, abc). Then the command will slightly change:
- 2. If you need to find only a specific word and not words with a common root, you will need this command:
- 3. You can also search for not one, but two and more words:
These are just a few examples of using grep in the shell script, but in general, it is a very extensive topic with many additional features.
How to open Linux files on Windows?
DiskInternals Linux Reader must be with all Linux owners.
This software will be needed if you are using a virtual machine or dual-boot, and you need to get files from Linux to Windows - and this, from experience, will be needed sooner or later. The program supports Ext2/3/4, ReiserFS, Reiser4, HFS, HFS, etc.
Here, read-only access to files on Linux disks is possible, while the source files will remain the same. You can also use the preview before transporting files, at no cost.
Based on the foregoing, DiskInternals Linux Reader is an unrivaled assistant for owners of dual boot or VM.