![]() |
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 |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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.. |
|
||||
|
Perfect!!.. This is what I was looking for. Thanks!
|
|
||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl, solaris |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|