grep : Linux Command

Rate this post

Searching Inside the File/Files in Linux 

  • grep prints the matching lines

Let’s have some example to understand this:

Example 1: To Search for the given string in a single file 

Let me create a file eduguru.sh.

$ cat eduguru.sh
#!/bin/bash
fun()
echo “This is a test.”
# Terminate our shell script with success message
exit 1
fun()

Now from above file (eduguru.sh) , we will search the text “exit”

$ grep “exit” eduguru.sh

output:

exit 1

Example 2: To Search for the given string in multiple files

Let’s create 2 files.

$ cat eduguru.sh
#!/bin/bash
fun()
echo “This is a test.”
# Terminate our shell script with success message
exit 1

fun()

$ cat edugurublog.sh
#!/bin/bash
fun()
echo “This is a test1.”
# Terminate our shell script with success message
exit 0

fun()

Now we will search “exit” text in both the files.

$ grep exit eduguru*

output:

edugurublog.sh:       exit 0
eduguru.sh:        exit 1

 

For any query or issue, feel free to discuss on http://discuss.eduguru.in

Leave a Reply

%d bloggers like this: