I have to grep a pretty huge text file (~15GB) for 3 different strings. The important thing to be noted is the three strings say X,Y and Z comprise about 99.9 % of the text file and are mutually exclusive.
i.e X + Y + Z + .01% junk = complete file
So Is there any way I can reduce the time by doing grep开发者_如何学编程 and grep -v function simultaneously Hence the flow of logic should be:
grep X filename.txt >> linescontainingstringX.
somehow do grep Y and grep Z on the remaining file ie grep Y >> linesnotcontainingstringX
Please let me know if there is a method for me.
Use perl.
perl -n -e 'BEGIN{ open XFILE,">x.txt" or die "$!" ; open YFILE,">y.txt" or die "$!"; open ZFILE, ">z.txt" or die "$!";} print XFILE $_ if /X/; print YFILE $_ if /Y/; print ZFILE $_ if /Z/;'
use egrep
and a regular expression that matches X OR Y OR Z.
http://www1.cs.columbia.edu/~tal/3261/fall07/handout/egrep_mini-tutorial.htm
egrep 'X|Y|Z` myFile.txt
精彩评论