Finding text around a keyword in linux files using grep
Grep, Linux September 9, 2014 No Comments on Finding text around a keyword in linux files using grepWhen 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.
Latest posts by Mark Walker (see all)
- The Quintessence of Cybersecurity and Stellar IT Support - November 9, 2023
- Choosing the Right Payment Processor for Your Website - March 3, 2022
- Choosing a business management system - June 11, 2020
Leave a comment
You must be logged in to post a comment.