I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in)
This is on solaris
Can you help?
I want to search a file for a string and then if the string is found I need the line that the string is on - but also the previous two lines from the file (that the pattern will not be found in)
This is on solaris
Can you help?
There is some option for grep itself for this same purpose like grep -An (for getting n lines above the searched pattern) and grep -Bn (for getting n lines below the searched pattern) ... not sure if this is there for solaris ... i think no ...
meanwhile u can try out this solution as well ...
grep -n pattern filename | cut -d":" -f1 | xargs -i expr {} - 2 | xargs -i sed -n '{},/pattern/ p' filename
Awk:
NR>3 { delete a[NR-3] }
/find this/ { print a[NR-2]; print a[NR-1]; print }
{ a[NR] = $0 }