awk or sed for finding closest pattern to a line number | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

awk or sed for finding closest pattern to a line number

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 10-10-2008
alirezan alirezan is offline
Registered User
Join Date: Aug 2008
Last Activity: 13 March 2009, 9:09 PM EDT
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
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:



I found the pattern say qqq to be on line 20. I want to find the first --- before pattern qqq (line 20) and I want to print the entire chunk from --- till the qqq pattern printed out.

Can someone help me out here please?
Sponsored Links
Old 10-11-2008
Franklin52's Avatar
Franklin52 Franklin52 is offline Forum Staff  
Join Date: Feb 2007
Last Activity: 30 January 2015, 6:35 AM EST
Location: The Netherlands
Posts: 7,722
Thanks: 127
Thanked 539 Times in 509 Posts
Should be something like:

awk '
/qqq/{for(i=1;i<=c;i++){print a[i]}exit}
' file

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] Finding the next line when a pattern matches mr_manii Shell Programming and Scripting 3 10-04-2011 06:52 AM
Closest Number from a Range of Numbers SkySmart Shell Programming and Scripting 6 08-16-2011 02:52 PM
finding the line number of a particular line in a file mvignesh Shell Programming and Scripting 3 07-06-2011 12:44 PM
delete rows between closest pattern or range sudheer1984 Shell Programming and Scripting 10 01-21-2010 09:26 AM
sed match closest/nearest pattern sudheer1984 UNIX for Advanced & Expert Users 1 07-02-2009 04:59 AM

All times are GMT -4. The time now is 12:38 PM.