Finding text around a keyword in linux files using grep

Finding text around a keyword in linux files using grep

Grep, Linux No Comments on Finding text around a keyword in linux files using grep

When searching log files grep is an extremely useful tool and one any linux administrator should know.

However the default output will normally be the matching line.

With certain files, for example an SQL export of a database one line could be many thousands or millions of characters long.

In these instances you need a method of extracting just the text surrounding the keyword.

grep -E -o ".{0,5}keyword.{0,5}" file.sql 

The {0,5} before and after the keyword specifies it will display 5 characters before and 5 characters after.

Play with these numbers to meet your requirements but keep them as small as possible when dealing with huge files since higher numbers will take up a lot more processing grunt.

Mark Walker

Related Articles

Leave a comment

You must be logged in to post a comment.

A collection of tips, tricks, reviews and discussion created by webmasters for webmasters.

Search

Back to Top