Difference between revisions of "Grep"

From TBP Wiki
Jump to: navigation, search
(Created page with "grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given...")
 
(Using grep)
 
(4 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
=Using grep=
 
=Using grep=
 
<strong>Recursively search DIR for "STRING" (also ignores caps)</strong>
 
<strong>Recursively search DIR for "STRING" (also ignores caps)</strong>
     grep -ri "STRING" /DIR/
+
     grep -irl "STRING" /DIR/
  
 
<strong>Piping</strong>
 
<strong>Piping</strong>
Line 14: Line 14:
  
 
     tail -f /var/log/somefile.log |grep specificstring
 
     tail -f /var/log/somefile.log |grep specificstring
 +
 +
    sudo cat /usr/local/apache/logs/error_log | grep -i "modsec" | awk '{print $10}' | sort | uniq -c | sort -n
 +
 +
<strong>Remove everything but grep'd</strong>
 +
 +
This will show only lines with "firstline" within file.txt.
 +
 +
    cat file.txt | grep '^firstline'
 +
 +
Recursively grep for STRING within /DIR using 8 threads in parallel:
 +
 +
  find /DIR -type f | parallel -k -j8 -n 1000 -m grep -H -n -ril STRING {}

Latest revision as of 10:55, 18 July 2019

grep searches the named input FILEs (or standard input if no files are named, or if a single hyphen-minus (-) is given as file name) for lines containing a match to the given PATTERN. By default, grep prints the matching lines. grep is a powerful tool.

Using grep

Recursively search DIR for "STRING" (also ignores caps)

    grep -irl "STRING" /DIR/

Piping

grep can also be piped from or to other outputs using the pipe "|"

   grep "STRING" /LOCATION/logfile.log |grep ANOTHERSTRING

Example usage:

   grep "13:00:" /var/log/messages |grep "September"
   tail -f /var/log/somefile.log |grep specificstring
   sudo cat /usr/local/apache/logs/error_log | grep -i "modsec" | awk '{print $10}' | sort | uniq -c | sort -n

Remove everything but grep'd

This will show only lines with "firstline" within file.txt.

   cat file.txt | grep '^firstline'

Recursively grep for STRING within /DIR using 8 threads in parallel:

  find /DIR -type f | parallel -k -j8 -n 1000 -m grep -H -n -ril STRING {}