Grep capping size of results lines, for multiple grep phrases?

63 Views Asked by At

I am grepping for terms like this:

grep 'term1\|term2\|term3\|term4'

and I want to say, for all lines which are found, cap the results to 100 characters. I found some examples how to do this when searching for one term, but none accommodating the OR-ing of search terms.

2

There are 2 best solutions below

0
On BEST ANSWER

Try this with GNU grep:

grep -E 'term1|term2|term3|term4' file | grep -oE '^.{100}'
0
On

Just use awk:

awk '/term1|term2|term3|term4/{print substr($0,1,100)}' file