The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Operating Systems > SUN Solaris
.
google unix.com



SUN Solaris The Solaris Operating System, usually known simply as Solaris, is a free Unix-based operating system introduced by Sun Microsystems .

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with "grep", need to display several lines Draken UNIX for Dummies Questions & Answers 9 04-04-2008 03:46 AM
Display lines of the file on a log sbryant Shell Programming and Scripting 2 02-26-2008 02:16 PM
display all lines shary Shell Programming and Scripting 3 02-17-2007 04:09 PM
Make grep -c display like grep -n? Jerrad Shell Programming and Scripting 2 08-25-2006 12:20 AM
display no of empty lines atticus Shell Programming and Scripting 5 03-22-2006 03:35 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #8 (permalink)  
Old 07-23-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
Code:
awk:
nawk -v ran=$1 -v pat=$2 '{
arr[NR]=$0
if (index($0,pat)!=0)
        line=NR
}
END{
for(i=line-ran;i<=line+ran;i++)
        print arr[i]
}' file

perl:
$pat=shift;
$ran=shift;
open FH,"<a";
while(<FH>){
$arr[$.]=$_;
if (index($_,$pat)>=0){
$line=$.;
}
}
close(FH);
for($i=$line-$ran;$i<=$line+$ran;$i++)
{
print $arr[$i];
}

Last edited by summer_cherry; 07-23-2008 at 02:11 AM..
  #9 (permalink)  
Old 09-25-2008
cv_pan cv_pan is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 12
Quote:
Originally Posted by Ygor View Post
Try...
Code:
nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1
...where "b" and "a" are the number of lines to print before and after string "s".
Perfect!!.. This is what I was looking for. Thanks!
  #10 (permalink)  
Old 02-02-2009
uttamhoode uttamhoode is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 18
awesome!!!
  #11 (permalink)  
Old 02-18-2009
anduzzi anduzzi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 48
nawk - header

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=2 a=4 s="string" file1

This one liner really helped me to get what I want but there is little deviant to my problem...

I want to search for a string and go back several lines(the number of lines is not fixed) and catch a pattern which is associated with the searched string.
for eg:
<HEADER>
line1
line2
line3
line4
............
<string>
I want to somehow associate <HEADER> line once I find my <string> and discard the rest of results..As I said, the number of lines is not fixed till we traverse back to <HEADER> line.

Is there any way we can achieve this?.

-Anduzzi
  #12 (permalink)  
Old 02-18-2009
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
Try...
Code:
awk '$0~s,$0~t' s="<HEADER>" t="<string>" file1
  #13 (permalink)  
Old 02-19-2009
anduzzi anduzzi is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 48
Could you please explain the command and for some reason the syntax doesnt quite work for me ?.

Thx !
  #14 (permalink)  
Old 02-19-2009
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
  
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,407
http://people.cs.uu.nl/piet/docs/naw..._51.html#SEC54
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl, solaris

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0