Saturday, December 12, 2015

Shell (4): grep (1) .......

#Options: grep --help, grep -V, grep --version, grep -E, grep -F, grep --color


PATTERN MATCH
#To extract all lines that start (^) with the required pattern (# here)
grep "^#" file
grep "protein" file
#Extract all rows with the pattern 'bc'
grep '>*bc' file
#Show the line number and lines with the pattern
grep -n '>*bc' file
#Extracts all line with this alphabet
grep 'y* ' file
#Extracts all line ending with y
grep 'yy* ' file
#Extract all lines with word ston
grep ston file
#Extract all lines with word ston in files starting f
grep ston f*
#print line with pattern and 2 lines before it
grep pattern  -B 2 file
#print line with pattern and 1 line after it
grep island  -A 1 file
#greps line with the full word
grep -iw pattern file
grep -ic pattern file
#Extract all lines with starting alphabet 'Mn' or 'pattern' in file
grep '^M' file
grep -i "^pattern" file
#Extract all lines with pattern starting with zip
grep -h '^zip' file*
#Extract all lines with pattern ending with zip
grep -h 'zip$' file*
#Extract all lines with pattern in between ^ and  $ 
grep -h '^zip$' file*
#Extract all lines with pattern b or g before zip
grep -h '[bg]zip' file*
#Extract lines with starting with either C or T
grep '^[CT]' file
#Extract lines with values of 0 to 9
grep  '[0−9]' file
#Extract lines with a number 2 followed by a period or comma, then  a number. It will work for any other punctuation like comma, colon e.g. grep '2\,.' file
grep '2\..' file
#Extract lines with pattern .7
grep '\.7' file
#Prints line with either 1 or both patterns (suppose the patterns are 'tyu' and 'uju')
grep "tyu\|uju" file                      #The \ symbol is important)
grep -E "tyu|uju" file
grep -e tyu -e uju file
egrep "tyu|uju" file
#Prints all the lines in a file, that match any pattern
grep -E 'pattern1.*pattern2' file
grep -E 'tyu.*uju|tyu.*uju' file
grep 'tyu' file | grep 'uju' file
#Search full words only
grep -w "pattern" file
#Find pattern ignoring case
grep -i "pattern" file
#Find pattern recursively i.e. read all files under each directory 
grep -r "pattern" dir
#Suppress inclusion of the file names in the output data
grep -h -R "pattern" dir
#Extract the portion between the two patterns
grep ’^pattern1.*pattern2$’ file
#Number of lines containing t times s in them
[jacob@moku ~]$ grep ’l.*l.*l.*l.*l’ file
Count (-c)
Count the number of fasta sequences in the file
grep ">" file | wc -l
grep -c '>' file
BEFORE, AFTER (-A, -B)
Print a line after the pattern
grep -A1 "pattern" file
Print the line with pattern, 1 line before and 2 line after
grep -B1 -A2 "pattern" file
#To extract the line with given pattern and line just next to it
grep -A 1 "b" file
#Show 3 lines after the first line with the given pattern
grep -A 3  '>*bc' file
#Show 2 lines before the pattern, and 1 line after the pattern.
grep -B2 -A1 pattern file
NOT PRESENT (-v)
#Prints all the lines, that do not match a given pattern
grep -v 'tyu' file
grep -v XYZ file
#Remove blank lines from the file
grep -v "^$" file
# grep -q option prevents display of the match on monitor.

No comments:

Post a Comment