The right way to Use the Grep Command in Linux

by | Nov 16, 2023 | Etcetera | 0 comments

In the world of Linux, taking a look through text information to hunt out particular content material subject material is a not unusual task, and one that can be finished effectively with the grep command. Fast for “Global Not unusual Expression Print,” grep is a powerful command-line device that allows shoppers to seem through information the use of patterns defined via not unusual expressions.

Whether or not or no longer you’re on the lookout for a decided on error in a log document, or taking a look to search out all cases of a specific period of time in a large codebase, grep is the go-to device for text taking a look and manipulation. Being able to are compatible complicated patterns, filter results, and even perform operations all the way through a few information, grep stands as the most important instrument for system administrators, programmers, and data analysts alike.

Not unusual syntax for grep command:

$ grep [OPTIONS...] [PATTERN] [FILE...]
1. Search for something inside a document
grep exp FileName.txt

grep is a powerful command that lets you search for a specific set of characters, or words exist in a document, or a few information. The command above search for exp inside FileName.txt, and return results when found out.

Apply: grep is thru default case-sensitive, and without other parameters involved, grep would return results as long as it fits “exp”.

Example:

Assuming that FileName.txt incorporates the following text:

This is an example document.
The word exp is correct right here.
No are compatible in this line.
Expression is a brilliant word.
Enjoy teaches wisdom.

The command grep exp FileName.txt would end result throughout the following output:

This is an example document.
The word exp is correct right here.
Expression is a brilliant word.
Enjoy teaches wisdom.

This output displays all the traces in FileName.txt that contain the substring “exp”.

2. Search for something in a few information
grep all name1.txt name2.txt name3.txt

This command expands taking a look to multilple specified filenames.

See also  How you can Make a Web page Banner (3 Simple Techniques)

Example:

The command grep all name1.txt name2.txt name3.txt uses grep to search for the string “all” all the way through the information name1.txt, name2.txt, and name3.txt. If the string is situated, it’ll print the traces containing that string along side the document names.

name1.txt:We are all in this together.
name2.txt:All of the very best for your long term.
name3.txt:all of the ones traces are compatible.
name3.txt:All is for sure.
3. Finding an exact word with grep
grep -w example Example.txt

With the -w parameter, grep gets additional precise in its search and perfect return true if the proper word fits. Inside the command above, grep search for “example” in Example.txt.

Any of the following would return false:

  • Example
  • examples
4. Case-insensitive search with grep
grep -i being ExampleFile.txt

With the -i parameter, grep will search in a case-insensitive way and will return true as long the input fits, regardles if it’s lowercase or uppercase characters.

The command above searches for the word “being” in ExampleFile.txt, and will return end result if found out.

All of the following will return true with life of -i:

  • Being”
  • “beING
5. Rely and output word repeatation with grep
grep -c smallness TextFile.txt

With the -c parameter, grep will first to find if a specific word exist, and then depend how over and over again it’s being repeated. The command above search for “smallness” and return the number of cases it existed in TextFile.txt.

Proper right here’s a hypothetical sample output for the given command:

5

This may suggest that the word “smallness” was once found in 5 traces all the way through the TextFile.txt document. If the word “smallness” isn’t found out throughout the document the least bit, the command would output:

0
6. Inverse search with grep
grep -v lorem sometext.txt

The parameter -v excludes all the line that matches the input pattern, and output the remaining that doesn’t contain it. The command above searches for “lorem” in sometext.txt. Any traces without “lorem” will return true.

See also  Tips on how to Display Document on Mac, Home windows 11, and Different Units

Example:

Imagine sometext.txt incorporates the following traces:

lorem ipsum dolor sit amet
consectetur adipiscing elit
lorem sed do eiusmod tempor

When you run the command grep -v 'lorem' sometext.txt, the output may well be:

consectetur adipiscing elit

This line is the only one who doesn’t contain the word “lorem.”

7. Display matching line and checklist line amount
grep -n ipsum randomtext.txt

The parameter -n returns content material subject material with line-count. When a search word is integrated, it returns all the line (where word exists) with its line-count. The command above search for “ipsum” in randomtext.txt, and its output presentations which line “ipsum” is at.

Example:

Assuming that randomtext.txt has the following content material subject material:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Each different line without the quest period of time.
However each and every different line.
ipsum ipsum ipsum
That is an ipsum too.

The command grep -n ipsum randomtext.txt would produce:

1:Lorem ipsum dolor sit amet, consectetur adipiscing elit.
4:ipsum ipsum ipsum
5:That is an ipsum too.

Proper right here, the numbers forward of the colons represent the street numbers throughout the document where the string “ipsum” was once found out.

8. Tick list filenames that contain matched string
grep -l dolor *txt

With the -l parameter, perfect .txt extension information that contain the word “dolor” will return true. Filenames may also be printed instead of all the lioe.

Example:

Assuming you are going to have 3 information throughout the checklist, particularly file1.txt, file2.txt, and file3.txt, and “dolor” is situated in file1.txt and file3.txt, the output would seem to be this:

file1.txt
file3.txt
9. Search traces starting with a pattern
grep ^Example TextFile.txt

The character ^ in front of a search-pattern suggests grep will have to perfect look words that starts with the search-pattern and no longer the rest. The command above will search in TextFile.txt, and return all traces that begins with “Example“.

See also  Why Does My iPhone Stay Turning On and Off? Best 10 Fixes for iOS 17/16

Example:

Assuming TextFile.txt incorporates the following text:

Example line 1
This is each and every different line
Example line 2
However each and every different line without the important thing word
Example line 3

The output of the command may well be:

Example line 1
Example line 2
Example line 3
10. Multiple pattern search with grep
grep -e lorem -e amet ExampleFile.txt

The -e parameter can be used a few cases within the an identical command; every paired with a search-pattern, permits you to be additional particular in on the lookout for something in a document. The command above searches for the words “lorem“, and “amet” in ExampleFile.txt, and return if true/found out.

Example:

Suppose ExampleFile.txt incorporates the following traces:

lorem ipsum dolor sit amet
consectetur adipiscing elit
amet, consectetur adipiscing
sed do eiusmod tempor
lorem incididunt ut

Running the command grep -e lorem -e amet ExampleFile.txt would output:

lorem ipsum dolor sit amet
amet, consectetur adipiscing
lorem incididunt ut
Further Linux directions:
Record Operations rmdir · cd · pwd · exa · ls
Report Operations cat · cp · dd · much less · contact · ln · rename · extra · head
Report Instrument Operations chown · mkfs · find
Networking ping · curl · wget · iptables · mtr
Search and Text Processing in finding · grep · sed · whatis · ripgrep · fd · tldr
Instrument Knowledge and Regulate env · historical past · most sensible · who · htop · glances · lsof
Client and Session Regulate display · su · sudo · open

The put up The right way to Use the Grep Command in Linux seemed first on Hongkiat.

WordPress Website Development

Supply: https://www.hongkiat.com/blog/linux-command-grep/

[ continue ]

WordPress Maintenance Plans | WordPress Hosting

read more

0 Comments

Submit a Comment

DON'T LET YOUR WEBSITE GET DESTROYED BY HACKERS!

Get your FREE copy of our Cyber Security for WordPress® whitepaper.

You'll also get exclusive access to discounts that are only found at the bottom of our WP CyberSec whitepaper.

You have Successfully Subscribed!