![]() |
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 |
| Finding the line number of matching braces | Rajendra_1510 | Shell Programming and Scripting | 3 | 10-07-2008 05:08 AM |
| Split File Based on Line Number Pattern | shankster | Shell Programming and Scripting | 11 | 10-01-2008 09:49 AM |
| Finding Last occurance of another pattern when a pattern is found. | rakeshou | Shell Programming and Scripting | 1 | 07-23-2008 03:29 PM |
| finding duplicate files by size and finding pattern matching and its count | jerome Sukumar | Shell Programming and Scripting | 2 | 12-01-2006 04:20 AM |
| Finding pattern & prepending a line with text | kirrushna | Shell Programming and Scripting | 1 | 07-11-2006 02:20 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
awk or sed for finding closest pattern to a line number
hi guys,
I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk between "---" and that line number. But here's the catch. the pattern "---" can be between 1 and 20 lines above that line number and I don't know how many lines above ... Also, another catch is that, "---" can happen multiple times in the file but I want the "CLOSEST" to the line number. Here's an example: Here's the file: Quote:
Can someone help me out here please? Thanks |
|
||||
|
Should be something like:
Code:
awk '
/---/{c=0}
{a[++c]=$0}
/qqq/{for(i=1;i<=c;i++){print a[i]}exit}
' file
|
| Sponsored Links | ||
|
|