GREP Command


grep, egrep (Extended GREP), fgrep (Fixed GREP), pgrep (Process GREP), rgrep (Recursive GREP).

All these variants have minor differences to original grep which has made them popular and to be used by various Linux programmers for specific tasks.

grep (Global Regular Expressions Print)

It searches for string in file. It can be used to find text in a file and search a directory structure of files recursively.

grep --color "files" test.txt

grep "files" test.txt



ifconfig | grep "inet"



grep -v "peoples" test.txt

Print all lines that do not contain the word "peoples"



grep -c "peoples" test.txt

It will only count lines that has word "peoples" in it. It does not matter how many times it appears on a single line it will count 1.

grep -o "string" file.txt

display only matched string

grep -n "string" file.txt

display the position of the line

  -i ignore case
  -h horizontal
  -R recursive

egrep (Extended GREP)



Tip: If you want grep to treat these characters as meta-characters, escape them with \ \?, \+, \{, \|, \(, and \). like you do in program.

pgrep (Process-ID)

pgrep looks through the currently running processes and lists the process IDs.

  pgrep firefox
  kill id
  killall firefox

zgrep

grabs text file from zip

fgrep (Fixed GREP)

Filters text which matches a fixed-character string.

Same as grep -F. In this mode, fgrep evaluates your PATTERN string as a "fixed string".

If your string contains an asterisk ("*"), fgrep will try to match it with an actual asterisk.

!grep or r grep

Runs the last executed grep command

No comments:

Post a Comment

Popular Posts