![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | 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 here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| SED Search Pattern and Replace with the Pattern | racbern | Shell Programming and Scripting | 4 | 03-15-2008 01:59 AM |
| Search Mulitiple String pattern in a file | krishnan_6015@y | Shell Programming and Scripting | 2 | 11-23-2007 12:03 AM |
| Reading lines in a file matching a pattern | torenji | Shell Programming and Scripting | 4 | 10-25-2007 01:15 AM |
| File search for pattern - script | rahulrathod | Shell Programming and Scripting | 3 | 02-16-2007 12:03 AM |
| Search a file for pattern? | davjoyce | UNIX for Dummies Questions & Answers | 2 | 03-07-2006 02:07 PM |
|
|
LinkBack | Thread Tools | 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? |
| Forum Sponsor | ||
|
|
|
|||
|
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 |