Searching a string in a file

Discussion in 'Unix' started by chie, Jun 1, 2014.

  1. chie

    chie Member

    Messages:
    44
    Likes Received:
    0
    Trophy Points:
    6
    The basic command to search for a string in a file is grep. However, not everyone knows that other UNIX commands can be used to search for a string.

    The sed command is normally used to execute scripts. The p action, along with -n, is used to print all matching lines.
    Example:
    $ cat test.txt | sed -n '/sample/p'

    The awk command is used for processing text in a file. There are different ways to print the string, it just depends on the format of the text file or approach of the code.
    Example:
    awk '/sample/' test.txt
     

Share This Page