Thursday

Pattern Searching

Commands Options Description
/ /Unix Forward search of Unix keyword in file
? ?Unix Backward search of Unix keyword in file
n   Repeat the last search
.   Repeat the previous command


   
:1, $s/<search string>/<Replace String >/g   Pattern search and replacement.
  1,$ Represent all lines in the file.
  g Stands for globally
:3,10s/gagan/deep/g   search between lines 3 and 10
: .s/gagan/deep/g   Only the current line.
: $s/gagan/deep/g   Only the last line.
: $s/gagan/deep/gc c Ask for confirmation for replacement


   
grep    
  -c counting occurrence.
  -n display line number for record.
  -v skip records that contain directory.
  -l display files containing record.
  -i ignore case.
  [PQR] match any single character.
  [c1-c2] match char with ASCII range
  [^PQR] match single character which is not PQR.
  ^<pat> beginning with pattern.
  <pat>$ ending with pattern.
  {a-Z A-Z 0-9} match any single character.
  ls –l |grep “^d” Prints only directories.


   
egrep    
  egrep '[aA]g+[ar][ar]wal' test1.txt match one or more occurrence matches ag & agg.
  egrep '[aA]gg?[ar][ar]wal' test1.txt match zero or one occurrence.
  egrep prashant|director test1.txt match eap1 or eap2  (finds prashant or line with director)
  egrep (das|sen)gupta test1.txt match exp x1x3 or x2x3 (like dasgupta & sengupta)
  egrep -f <pattern_file_name> test1.txt Huge list of pattern search can passed in the form of file name .pattern stored in file eg(prashant|admin|director)
  + Matches one or more occurrence of previous character.
  ? Matches zero or one occurrence of previous character.


   
Fgrep   fgrep and egrep accepts multiple pattern both form command line and a file but unlike grep and egrep  does not accept regular expression.
  fgrep -f pattern file emp file Faster than grep and egrep family

1 comment :

  1. This site is really awesome and informative. The presentation is nice. Thanks for the awesome some.
    Check out more Unix tutorial here

    ReplyDelete

Recent Comments