grep : Linux Command

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

 

Related Posts

  • 36
    rsync = Remote Sync This is a remote and local file synchronization tool. It uses an algorithm that minimizes the amount of data copied by only moving the portions of files that have changed. This is a very flexible network-enabled syncing tool. Due to its ubiquity on Linux and Unix-like…
    Tags: files, file, linux, command
  • 34
    cp command under a Linux, UNIX-like, and BSD like operating systems is used to copy files and directories . cp is the command entered in a Unix and Linux shell to copy a file from one place to another, possibly on a different filesystem. The original file remains unchanged, and the new…
    Tags: command, linux, file, files, shell
  • 33
    The vi Editor http://t4test.com/blog/unix-linux-shell-script-introduction-to-shell-scripting/ http://t4test.com/blog/read-making-shell-script-interactive/ http://t4test.com/blog/cpio-a-backup-program/ Backup Using TAR Backing up your files using tar is very simple you just type a little command. tar -cvpzf /BackupDirectory/backupfilename.tar.gz /ImportantData/directory/path Let’s suppose i have directory called /imp-data on root and i want to make backup of this directory including sub directories on…
    Tags: command, file, linux, shell, script, files

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: