![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum 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 |
| Finding the line number of matching braces | Rajendra_1510 | Shell Programming and Scripting | 3 | 10-07-2008 02:08 AM |
| Split File Based on Line Number Pattern | shankster | Shell Programming and Scripting | 11 | 10-01-2008 06:49 AM |
| Finding Last occurance of another pattern when a pattern is found. | rakeshou | Shell Programming and Scripting | 1 | 07-23-2008 12:29 PM |
| finding duplicate files by size and finding pattern matching and its count | jerome Sukumar | Shell Programming and Scripting | 2 | 12-01-2006 01:20 AM |
| Finding pattern & prepending a line with text | kirrushna | Shell Programming and Scripting | 1 | 07-10-2006 11:20 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Should be something like:
Code:
awk '
/---/{c=0}
{a[++c]=$0}
/qqq/{for(i=1;i<=c;i++){print a[i]}exit}
' file
|
|||
| Google The UNIX and Linux Forums |