Search inside file in linux

How to use grep in a shell script to find a word inside of a file

How can I use grep to find an exact word inside of a file entered by the user as string? For example I need to select the word I want to find and the file I want to find it in. I’ve been told I am really close but something is not working as it should be. I’m using bash shell under Linux. Here’s what I’ve done so far:

#!/bin/bash echo "Find the file you want to search the word in?" read filename echo "Enter the word you want to find." read word1 grep $word1 $filename 

4 Answers 4

How can I use grep to find an exact word inside of a file entered by the user as string.

Try using -F option. Type man grep on shell for more details.

It’s recommended to enclose search string and file name with quotes to avoid unexpected output because of white spaces.

Not sure why you have fi on the last line. It’s not needed.

grep -R WORD ./ to search the entire current directory, or grep WORD ./path/to/file.ext to search inside a specific file.

Welcome to Stack Overflow. Please read the About page soon. While the information you added about -R is new, it is not clear that it is relevant to the context of the question. The ‘or’ information is already understood. In short, you’ve not added anything useful to the information already available. You’re at liberty to add such answers, but they’re relatively unlikely to be upvoted and may earn you downvotes. When you add a new answer to an old question with existing answers, please make sure you’re adding something distinctive and new that’s relevant.

#!/bin/bash/ echo "Find the file you want to search the word in?" read filename echo "Enter the word you want to find." read word cat $filename | grep "$word" 

This works fine to find the exact word match in a file.

Источник

The “grep” Command in Linux [10+ Practical Examples]

The grep command is very useful for searching files and directories containing matching words or characters lines. You will also be able to know the pathname of the files, and line numbers with it. In this article, I will describe all the features and uses of the grep command in Linux with multiple examples.

Читайте также:  Meterpreter payload for linux

A. Description

The grep command can be used to search patterns in specific files or each file. Then prints the entire line containing the match. It is the shortcut for “Global Regular Expression Print

B. Syntax

The syntax for the grep command is pretty simple which is the command itself followed by some specific options and the pattern you want to grep.

grep [OPTION]. PATTERNS [FILE…]

Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time. Then some PATTERN you want to search for. Moreover, 3 dots after FILE suggest that you can specify multiple files to search through.

C. Options

Many useful options are available for the grep command. I have listed some of the most used ones here. However, if you want to know more about options for the grep command you can always check the man page for the grep command.

  • -c, –count (Does not print any matches but rather the total occurring number)
  • -i, –ignore-case(Ignores the sensitivity of cases)
  • -v, –invert-match (Invert the sense of matching, to select non-matching lines.)
  • -V, –version (Output the version number of grep and exit.)
  • -w, –word-regexp (search for a whole word)

NB: The options in Linux CLI(Command Line Interface) are all case-sensitive, So be cautious while using them.

Practical Examples of the “grep” Command in Linux

The grep command in Linux is an easy-to-use command which is used to search for words or characters matching lines in files. Some of the most useful applications of the grep command have been illustrated below.

Example 1: Search Inside a File Using the “grep” Command in Linux

You can search for a specific word or character inside a file using the grep command. To search for any word inside a file you should follow the below procedure. Here, I have searched for the word “Linux” in a file named file1.txt.

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

➌ Press the ENTER button.

Search Inside a File Using the “grep” Command in Linux

You can see in the below picture that all the lines are printed containing the matching word Linux.

Similar Readings

Example 2: Search Inside Multiple Files Using the “grep” Command in Linux

You can search for a specific word or character inside multiple files using the grep command. To search for any word inside multiple files you should follow the below procedure. Here, I have searched for the word “Linux” inside 2 files named file1.txt & file2.txt.

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep Linux file1.txt file2.txt

➌ Press the ENTER button.

Search Inside Multiple Files Using the “grep” Command in Linux

You can see in the below picture that all the lines are printed containing the matching word “Linux” inside both files.

Example 3: Search Multiple Words Inside a File Using the “grep” Command in Linux

You can search for multiple words or characters inside a file using the grep command. To search for multiple words inside a file you should follow the below procedure. Here, I have searched for the words “Linux” & “love” inside a file named file2.txt.

Читайте также:  All commands in kali linux

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

grep -e Linux -e love file2.txt

➌ Press the ENTER button.

Search Multiple Words Inside a File Using the “grep” Command in Linux

You can see in the below picture that all the lines are printed containing both the matching words “Linux” & “love” inside the file.

Example 4: Search All Files Inside a Directory

You can search for a specific word or character inside all the files in a directory using the grep command. To search for any specific word inside all files in a directory you should follow the below procedure. Here, I have searched for the word “Linux” in all the files in the Desktop directory using the asterisk (*) sign.

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

➌ Press the ENTER button.

Search All Files Inside a Directory

You can see in the below picture that all the lines are printed containing the matching word Linux inside to files of the Desktop directory.

Similar Readings

Example 5: Finding Whole Words Only Using the “grep” Command in Linux

Normally grep gives you the matching words as well as the substring of the matching words. You can search for only the whole word or character inside a file using the grep command. To search for the whole word only inside a file you should follow the below procedure. Here, I have searched for the whole word “Linux” only inside my home directory.

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

➌ Press the ENTER button.

Finding Whole Words Only Using the “grep” Command in Linux

You can see in the below picture that all the lines are printed containing the matching of the whole word Linux only. It has not highlighted the word LinuxSimply as here Linux is a substring of the word.

Example 6: Searching Inside Sub-directories Using the “grep” Command in Linux

You can search for a specific word or character inside sub-directories using the grep command. To search for any word inside a subdirectory you should follow the below procedure. Here, I have searched for the word “Linux” in my home directory.

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

➌ Press the ENTER button.

Searching Inside Sub-directories Using the “grep” Command in Linux

You can see in the below picture that all the lines are printed containing the matching word Linux. All the files in the sub-directory and their pathname is also printed here.

Example 7: Display Line Numbers Using the “grep” Command in Linux

You can search for a specific word or character inside a file and also show the line numbers using the grep command. To search for any word inside a file and show the line numbers you should follow the below procedure. Here, I have searched for the word “Linux” in a file named file1.txt.

Читайте также:  Amd ati linux install

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

➌ Press the ENTER button.

Display Line Numbers Using the “grep” Command in Linux

You can see in the below picture that all the lines are printed containing the matching word Linux. And their line numbers are also printed here.

Example 8: Display the Number of Lines Before and After

You can search for a specific word or character inside a file and also show the number of lines before, after and both using the grep command. To search for any word inside a file and show the number of lines, you should follow the below procedure. Here, I have searched for the word “Linux” in a file named file1.txt.

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

Display two Lines Before using the grep

➌ Press the ENTER button.➍ Type the following command in the command prompt:

Display two Lines After using the grep

➎ Press the ENTER button.➏ Type the following command in the command prompt:

➐ Press the ENTER button.

You can see in the first picture 2 lines are printed before the matching word line for the first command.

For the second command, 2 lines after the matching word line are printed in the second image.

Display two Lines Before and After

And for the last command, 2 lines after and before both are printed in the third picture.

Similar Readings

Example 9: Ignore Case-Sensitivity

The grep command by default doesn’t show the matching words if the name’s cases are wrong. To ignore the case sensitivity you should follow the below procedure. Here, I have used the file2.txt file to show you the example.

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

➌ Press the ENTER button.

Ignoring Case-Sensitivity

You can see in the below picture that all the lines are printed containing the matching word Linux although the case in the command line is not correct.

You can search inside a file where the line containing the given word or character will not be printed. To do so you should follow the below procedure. Here, I have given the word “Linux” in a file named file1.txt, where I do not want to see the lines containing the given word.

Steps to Follow:

➊ At first open the Ubuntu Terminal.

➋ Type the following command in the command prompt:

➌ Press the ENTER button.

Inverse grep Search

You can see in the below picture that all the lines are printed except the lines containing the matching word Linux.

Conclusion

After completing this article, you will acquire all the knowledge about the grep command in Linux. With the given practical examples, you will be able to execute many commands to grep the location of different matches on your machine.

Similar Readings

  • The “paste” Command in Linux [6 Practical Examples]
  • The “split” Command in Linux [6 Practical Examples]
  • The “sed” Command in Linux [7 Practical Examples]
  • The “tr” Command in Linux [6 Practical Examples]
  • The “uniq” Command in Linux [6 Practical Examples]

Источник

Оцените статью
Adblock
detector