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.
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.
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.
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 ----------