Get previous and next line

I'm trying to figure out how to do this I need to search for pattern when found the previous line and next line should be captured if in case the next or previous line is also matching move to one step previous or ahead.
Also need to search for a pattern and exit once the 3rd occurence is being found.

Tried using awk and using getline function to get previous but couldnt get next line.Any variant of awk, sed or perl is appreciated.

please give your input file for better solution(and tell how you want to figure it out).

The pattern is ABC in below file.

ABC
NEW
ABC
ABC
NEW
OP
ABC

And.. what's the required output for your example?

search for ABC if its found its previous line and next line should be displayed if the next or previous line is same then its further next or previous line.

How about:

grep -A1 -B1 "some_string"

Sorry but I don't really get it. Post a sample output based on you example to illustrate your request. Much easier to understand when provided with input *and* output samples.

What is A1 and B1 ?

---------- Post updated at 11:39 AM ---------- Previous update was at 11:35 AM ----------

Here is the file and searching for NAME

NAME
ADDRESS
NO
NAME
OTHER
NEW
OP
NAME
FEW

The output should be ADDRESS NO OTHER OP FEW

Try this:

INPUT FILE:

$ cat file.txt
NAME
ADDRESS
NO
NAME
OTHER
NEW
OP
NAME
FEW
OTHER
NEW
NAME

SHELL SCRIPT:

 
$ cat file1
grep -n "NAME" file.txt | cut -d ':' -f1 > /tmp/occurance_lines.txt
i=1
while read line
do
  if [[ ${i} -gt 3 ]]; then
    break
  fi
  line1=$(( ${line} - 1 ))
  if [[ line1 -ne 0 ]]; then
    tail +${line1} file.txt | head -1 >> /tmp/output.txt
  fi
  line2=$(( ${line} + 1 ))
  tail +${line2} file.txt | head -1 >> /tmp/output.txt
  i=$(( ${i} + 1 ))
done < /tmp/occurance_lines.txt
cat /tmp/output.txt
rm -f /tmp/occurance_lines.txt /tmp/output.txt

OUTPUT:

 
$ ./file1
ADDRESS
NO
OTHER
OP
FEW

HTH,:cool:

Regards,

Praveen

-A1 option print the line "after" the matched line
-B1 option print the line "before" the matched line
You can also use -C1 which is equal to -A1 -B1

grep -C1 "something" yourfile

Is this what u expected ?

Here is what i get ..

% grep -C1 "ABC" pattern
grep: illegal option -- C
grep: illegal option -- 1
Usage: grep -hblcnsviw pattern file . . .

@praveen
can it be done using one liners ?

Perhaps in awk. But that is unfortunately, not my forte. :frowning:

awk experts, anyone?

Regards,

Praveen

Try...

 
awk '/NAME/{getline;print prev;print $0;next}{prev=$0}' infile

Can you explain it a bit.

@dinjo_jo your request is not clear. Like that you can use just grep

# grep -v NAME file
ADDRESS
NO
OTHER
NEW
OP
FEW

@malcomex999 your script yield first line empty.

 
awk '/NAME/{getline;    ###when you find the pattern NAME, get the next line
print prev;        ### print previous line from prev variable
print $0;next}     ### print current line and read next line
{prev=$0}' infile  ### assign current line to prev variable which will be previous line when read next line

---------- Post updated at 03:15 PM ---------- Previous update was at 03:08 PM ----------

@danmero
This fixes it...

 
awk '/NAME/{getline;if(prev!="")print prev;print $0;next}{prev=$0}' infile