![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SED Search Pattern and Replace with the Pattern | racbern | Shell Programming and Scripting | 4 | 03-15-2008 05:59 AM |
| Search Mulitiple String pattern in a file | krishnan_6015@y | Shell Programming and Scripting | 2 | 11-23-2007 04:03 AM |
| Reading lines in a file matching a pattern | torenji | Shell Programming and Scripting | 4 | 10-25-2007 04:15 AM |
| File search for pattern - script | rahulrathod | Shell Programming and Scripting | 3 | 02-16-2007 04:03 AM |
| Search a file for pattern? | davjoyce | UNIX for Dummies Questions & Answers | 2 | 03-07-2006 06:07 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
Search file for pattern and grab some lines before pattern
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:
Code:
NR>3 { delete a[NR-3] }
/find this/ { print a[NR-2]; print a[NR-1]; print }
{ a[NR] = $0 }
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|