Tutorials Hut

Tutorials Hut

  • Unix For Testers

       Introduction to Unix
       Unix System Architecture
       Unix File System Structure
       Absolute and Relative Path
       Basic Unix Commands
       cal command in Unix
       who command in Unix
       date command in Unix
       clear command in Unix
       more command in Unix
       whoami command in Unix
       uname command in Unix
       man command in Unix
       echo command in Unix
       Unix File System Commands
       ls command in Unix
       cat command in Unix
       cp command in Unix
       mv command in Unix
       pwd command in Unix
       cd command in Unix
       mkdir command in Unix
       rmdir command in Unix
       rm command in Unix
       touch command in Unix
       dirname command in Unix
       tar command in Unix
       Unix Links(ln)
       Hard Link
       Soft Link
       Regular Expressions
        Basic Regular Expressions
       Interval Regular Expressions
       Pipes and Filters
       Unix Text Processing Commands
       cmp command in Unix
       diff command in Unix
       comm command in Unix
       cut command in Unix
       Paste command in Unix
       head command in Unix
       tail command in Unix
       wc command in Unix
       sort command in Unix
       grep command in Unix
       Process Related Command
       top command in Unix
       ps command in Unix
       nice command in Unix
       Kill command in Unix
       nohup command in Unix
       time command in Unix
       File Tranfer Commands in Unix
       file transfer using scp command
       file transfer using rlogin command
       file transfer using telnet command
       ssh(Secure Shell) command in Unix
       ftp file transfer command
       sftp file transfer command
       chmod command in Unix
       File Permission and File Security in Unix



  • grep Command in Linux/Unix

    grep command in Unix is used for pattern or expression searching. It has many OPTIONS that allows to perform various file search operations.

    In this article, we will learn common grep command usage with examples:

    grep command in Linux/ Unix

    • Grep full form is “Globally Search For Regular Expression and Print out”.
    • It  is a pattern or expression searching command.
    • Prints found matches.
    • Grep comes with a number of OPTIONS that allows to perform various search operations on files.
    grep command in Unix
    SYNTAX:
    $grep[options] "pattern to be matched" [filename ]

    Options               Use
    -v                             Shows all the lines that do not match the searched string
    -c                             Displays only the count of matching lines
    -n                             Shows the matching line and its number
    -i                              Match both (upper and lower) case
    -l                              Shows just the name of the file with the string

    Example:

    $grep 'hello' file1.txt
    Output : searches hello in the file1.txt and outputs/returns the lines containing 'hello'.

    Grep command can also be used with meta-characters:

    Example:

    Input : $grep 'test' *
    Output : it searches for test in all the files and directories.
    * is a meta-character and returns matching 0 or more preceding characters

    -n (–line-number) OPTION with grep command

      • It prints out the matches for the text along with the line numbers.
      • Lsts line numbers
    •  

    Example:

    $ grep -n 'root' /etc/passwd
    Output:
    1:root:x:0:0:root:/root:/bin/bash
    1042:rootmask:x:0:0:rootmask:/home/rootmask:/bin/csh

    The results has line numbers for the text matches.

    -c (–count) OPTION with grep command in Unix

    • It prints the number of lines of matches

    Example:

    $grep -c 'test' file1.txt
    Output:
    2
    Note: If there are more than 1 searched word 'test' on line one, option -c would still print 2. 
    This is because it is concerned with the number of lines where the matches appear, not the number of matches.

    -v (–invert-match) OPTION with grep command in Unix

    • -v option inverts the match, it matches only those lines that do not contain the given word.

    Example:

    $grep -v 'test' file1.txt
    Result:
    Hi All
    We are learning

    The output does not contains the searched pattern.
    Note :You can use -n option along with -v t list the line numbers.

    -i (–ignore-case) OPTION with grep command in Unix

    • It is used to ignore-case sensitivity

    Example:

    $grep -i 'tEst' file1.txt
    No output
    $grep -i 'tEst' file1.txt
    Output:
    In test
    Test class

    -l (–files-with-matches) OPTION with grep command

    • It print file names that match a pattern

    Example:

    $grep -l test *.txt
    Output:
    file1.txt

    -w (–word-regexp) OPTION with grep command 

    • It searches for the line containing the exact matching word.
    • By default, grep matches strings which contain the specified pattern.
    • This means that grep ‘test’ file1.txt will print the same results as grep ‘est’ file.txt because ‘est’ can be found in test.
    • With the option -w, grep ensures that the matches are exactly the same pattern as specified.

    Example:

    $grep -w est file1.txt 
    Output: No results
    $grep -i -w test file1.txt 
    Output:
    In test
    Test class

    -o (–only-matching) OPTION with grep command 

    • It prints only the matched pattern
    • By default, grep prints the line where the matched pattern is found.
    • With option -o, only the matched pattern is printed line by line.
    $grep -i -o yo file1.txt
    Output:
    test
    Test

    -r (-recursive) OPTION with grep command

    • It is used for recursive search.
    • By default, grep cannot search directories, you will get an error (“Is a directory”).
    • With option -R, searching files within directories and subdirectories becomes possible.

    Example:

    $grep -r test *
    Output:
    myData/file1.txt: In test
    myData/file2.txt: Test to learning

    for more example refer here

    Recommended Articles:

















  • Scroll to Top