User Tools

Site Tools


products:ict:linux:grep

Sure, I'd be happy to provide you with a detailed tutorial on using the `grep` command along with examples! `grep` is a powerful command-line tool used for searching patterns within text files. It stands for “Global Regular Expression Print.” Regular expressions (regex) are patterns that describe sets of strings. Let's dive into the tutorial with examples:

### Basic Syntax: ``` grep [options] pattern [file…] ```

- `pattern`: The regular expression you want to search for. - `file`: The file(s) in which you want to search for the pattern. If not provided, `grep` searches standard input.

### Common Options: - `-i`: Ignore case (case-insensitive search). - `-v`: Invert the match (display lines that do not match the pattern). - `-r` or `-R`: Recursively search directories. - `-l`: Display only the names of files containing the pattern. - `-n`: Display line numbers along with matching lines.

### Examples:

1. Basic Text Search:

 ```
 grep "apple" fruits.txt
 ```
 This searches for the word "apple" in the `fruits.txt` file.

2. Case-Insensitive Search:

 ```
 grep -i "banana" fruits.txt
 ```
 This searches for the word "banana" in a case-insensitive manner.

3. Search in Multiple Files:

 ```
 grep "orange" fruits.txt fruits_backup.txt
 ```
 This searches for the word "orange" in both `fruits.txt` and `fruits_backup.txt`.

4. Invert the Match:

 ```
 grep -v "pear" fruits.txt
 ```
 This displays lines in `fruits.txt` that do not contain the word "pear".

5. Search Recursively:

 ```
 grep -r "grape" /path/to/directory
 ```
 This searches for the word "grape" in all files under the specified directory.

6. Display File Names with Matches:

 ```
 grep -l "cherry" fruits_*.txt
 ```
 This lists the names of files (matching the pattern `fruits_*.txt`) that contain the word "cherry".

7. Display Line Numbers:

 ```
 grep -n "melon" fruits.txt
 ```
 This displays lines containing the word "melon" along with their line numbers.

8. Using Regular Expressions:

 ```
 grep "a[a-z]*e" words.txt
 ```
 This searches for words in `words.txt` that start with 'a' and end with 'e', with any number of lowercase letters in between.

Remember that regular expressions can get quite complex. The above example only scratches the surface of what's possible.

These are just a few examples to get you started with using `grep`. The command is versatile and can be used in various scenarios for efficient text pattern searching. Make sure to refer to the `grep` manual (`man grep`) for more details and options.

Difference Between grep and egrep

The main difference between grep and egrep is that grep is a command that allows searching content according to the given regular expression and displaying the matching lines while egrep is a variant of grep that helps to search content by applying extended regular expressions to display the machining lines.

The operating system is the core of the entire computer system. It works as the interface between the user and the hardware. Furthermore, it performs memory management, process handling, task scheduling and many more. UNIX is a stable operating system. Linux is a free and open source operating system that is based on UNIX. These operating systems provide the opportunity for the users to enter commands to the Command Line Interface (CLI) to accomplish tasks. There is a large number of commands with options. Two of them are grep and egrep. The grep allows to search patterns using regular expressions whereas egrep allows using extended regular expressions. Overall, egrep allows searching multiple patterns at a time using a single command easily.

After 104 commits from six different people, GNU grep was released Saturday, reports Phoronix.

The biggest change? “It's now made more clear that if you are still relying on the egrep and fgrep commands, it's past due for switching to just grep with the appropriate command-line arguments.” The egrep and fgrep commands have been deprecated since 2007. Beginning with GNU Grep 3.8 today, calling these commands will now issue a warning to the user that instead they should use grep -E and grep -F, respectively.

Eventually, GNU Grep will drop the egrep / fgrep commands completely but there doesn't seem to be a firm deadline yet for when that removal will happen.

From grep's updated manual: 7th Edition Unix had commands egrep and fgrep that were the counterparts of the modern 'grep -E' and 'grep -F'. Although breaking up grep into three programs was perhaps useful on the small computers of the 1970s, egrep and fgrep were not standardized by POSIX and are no longer needed. In the current GNU implementation, egrep and fgrep issue a warning and then act like their modern counterparts; eventually, they are planned to be removed entirely.

If you prefer the old names, you can use use your own substitutes, such as a shell script…

Other notable changes from the release announcement:

  The confusing GREP_COLOR environment variable is now obsolescent. Instead of GREP_COLOR='xxx' use GREP_COLORS='mt=xxx'
  Regular expressions with stray backslashes now cause warnings
products/ict/linux/grep.txt · Last modified: 2023/08/26 11:13 by wikiadmin