grep返回匹配上下文的办法

grep -B 10 "PATTERN" FILENAME
grep -A 10 "PATTERN" FILENAME

#返回匹配的行以及匹配行之前的10行
$ grep -B 10 'universe' myFirstfile.txt 
The greatest challenge is sifting through all the data that is generated by the
simulation.  In fact, it is impossible.  Impossible because to record every
event everywhere in the artificial universe would take more hard drive space

#返回匹配的行以及匹配行之后的10行
$ grep -A 10 'universe' myFirstfile.txt
event everywhere in the artificial universe would take more hard drive space
than was physically possible to create given Earth's dwindling resources.
Therefore, my life's greatest achievement was to encode a method to filter the
data, so only the most relevant events related to what we want is recorded. In
our case, that means space travel.  But not just any space travel, space travel
between galaxies.

你可能感兴趣的:(grep返回匹配上下文的办法)