All about Grep command

Grep is a very common question and it is widely used on day to day basis for searching text in log files. Hence it is very commonly asked in tech interview. This blogpost will help you understand most basic and commonly used options of grep command and its usage.

  1. Grep    -A           10         “Box”              File.tx
    1. It will print 10 lines after the string “Box” and the string “Box” equaling the number of times it occurred in the file.
  2. Grep    -B           10         “Box”              File.txt
    1. It will print 10 lines before the string “Box” and the string “Box” equaling the number of times it occurred in the file.
  3. Grep    -b           “Box”              File.txt
    1. It will print the byte offset before the each occurrence of the string in the file.
  4. Grep    -c           “Box”              File.txt
    1. It will print the count of lines where the string matches
  5. Grep    -v           “Box”              File.txt
    1. It will print the lines that are not matching.
  6. Grep    -c -v   “Box”              File.txt
    1. It will print the count of the no. of lines that are not matching the string “Box”.
  7. Grep    -D          skip/read           Device name
    1. It will read or skip the device, whose name is specified
  8. Grep    -E           “Box”              File.txt
    1. It will interpret the expression as an extended regular expression.
  9. Grep    -H          “Box”              *
    1. It will give you the names of the files along with the matching string. Generally filename is appended at the beginning, when multiple files are searched for.
  10. Grep    -h           “Box”              *
    1. Supress the filename
  11. Grep    -i            “Box”              *
    1. Ignore case. It will output both “Box” and “Box”
  12. Grep    -L           “Box”              *
    1. It will output the files where the string “Box” is not present
  13. Grep    -l            “Box”              *
    1. It will output the files where the string “Box” is present
  14. Grep    -line     “Box”              *
    1. Use line buffering, it could be performance penalty.
  15. Grep    -m         2            “Box”              *
    1. It will output m = 2 number of times the string “Box” from each file.
  16. Grep    -n           “Box”              *
    1. Prefix each line of output with the line number.
  17. Grep    -o           “Box”              *
    1. Output only the string Box instead of the entire line, where the matching string is available.
  18. Grep    -P           “B[a-z]arti”                         *
    1. Interpret the search string as a pattern for regular expression
  19. Grep    -r            “Box”              *
    1. Recursively look for the string Box in all folders and sub-folders
  20. Grep    -R           “Box”              *
    1. Recursively look for the string Box in all folders and sub-folders
  21. Grep    -q           “Box”              *
    1. Don’t output anything if the match is found or even if an error occurs. Just be silent.
  22. Grep    -s           “Box”              Box.txt
    1. If the string Box.txt is not present then the option –s will suppress this error. –s will suppress the error about non-existent and unreadable files
  23. Grep    -U          “Box”              *
    1. Treat the files as binary
    2. Note: From the website.
  24. Grep    -u           Box *
    1. It will output the bytes where the string is present. It works only on unix OS. It has no effect on Windows. To output the bytes we should use –b option
  25. Grep    –V
    1. It will output the version of the grep command
  26. Grep    -v           Rocks                *
    1. It will output the lines where the string Rocks is not present.
  27. Grep    -w          Rocks                *
    1. It will match only those words which are full words on the whole and not the string where Rocks is a part of the whole word
  28. Grep    -x           “This is good.”   *
    1. It will output only those lines which are exact match and not other lines.
  29. B{3}h{4}[ha]{5} -> bbbhhhhhahahaha
    1. You need \ character before the characters {,},(,),+,|

No comments:

Post a Comment

NoSQL

This one is reviewed but I need to delete its copy from hubpages or somewhere NoSQL Data models: key-value  Aggregate model.  key or i...