Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I want to grep with multiple arguments where i want to show lines in the order they appear in my source file before one argument and lines after the other argument , i e combine:

grep -A5 onestring foo.txt

and

grep -B5 otherstring foo.txt

How can this be achieved?

share|improve this question
    
Run grep twice. – Arthur2e5 yesterday
    
Yes but then I dont know the order between the matches. I am looking at at log at want to see what happens before otherstring and after onestring. So the order is highly relevant. – kungjohan yesterday
    
Hmm. ( grep -nA5 onestring foo.txt; grep -nB5 onestring foo.txt ) | sort -gu then. (Run grep twice with line numbers and uniquely sort them) – Arthur2e5 yesterday
    
Good idea. But will this work if I want to do this with multiple files? – kungjohan yesterday
    
We can ask grep to print filenames additionally or write a shell-function with some for loops. – Arthur2e5 yesterday

1 Answer 1

up vote 4 down vote accepted

In bash, ksh or zsh:

sort -gum <(grep -nA5 onestring foo.txt) <(grep -nB5 otherstring foo.txt)
# Sort by general numbers, make output unique and merge sorted files,
# where files are expanded as a result of shell's command expansion,
# FIFOs/FDs that gives the command's output

This takes O(n) time, given that grep outputs already sorted things. If process substitution is unavailable, either create temp files manually or use the O(n lgn) ( grep -nA5 onestring foo.txt; grep -nB5 otherstring foo.txt ) | sort -gu.

With grep -H we need to sort it in a more verbose way (thanks to cas):

# FIXME: I need to figure out how to deal with : in filenames then.
# Use : as separator, the first field using the default alphabetical sort, and
# 2nd field using general number sort.
sort -t: -f1,2g -um <(grep -nA5 onestring foo.txt bar.txt) <(grep -nB5 otherstring foo.txt bar.txt)
share|improve this answer
    
grep's output isn't sorted, rather the log file input is pre-sorted by date and time. i assume that's what you meant, but the way you wrote it isn't right. – cas yesterday
    
@cas They are, in the sense of line numbers. We are using sort to put the lines back into the original order. – Arthur2e5 yesterday
    
btw, when using grep -H you can use sort's -t: and -k options to skip sorting on the filename etc fields. – cas yesterday
    
@cas awesome and thanks. – Arthur2e5 yesterday
    
right, so you are. missed that, my mistake – cas yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.