![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem in the return of grep | mail2sant | Shell Programming and Scripting | 1 | 04-11-2008 04:00 AM |
| return the previous line | user_prady | Shell Programming and Scripting | 12 | 12-18-2007 04:37 AM |
| using Lynx and Grep to return search page rank - help | roninuta | UNIX for Dummies Questions & Answers | 0 | 09-18-2007 09:27 AM |
| how to get return code in one line | bluemoon1 | Shell Programming and Scripting | 6 | 09-13-2007 07:09 PM |
| modifying grep to return latest files | ragha81 | Shell Programming and Scripting | 4 | 12-28-2006 01:09 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
grep for a line then return lines above
Hey guys,
I just want to grep for a line then return a few lines above it I can't seem to find what im looking for on google can someone help me out? This is on Solaris 9..... I don't have GNU grep so -B and -A commands will not work Last edited by kingdbag; 03-05-2007 at 03:01 PM. |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Grab GNU grep from sunfreeware.com
http://www.sunfreeware.com/programlistsparc9.html#grep You can try something like this although it's convoluted. Cheers ZB |
|
#3
|
|||
|
|||
|
Wow, do i really need to script something for grep to do this?
I found this and its supposed to return the above 3 lines when it finds FORWARD in the text file and it is blank... sed -n '/FORWARD/{g;3!p;};h' crap.txt Last edited by kingdbag; 03-05-2007 at 03:42 PM. |
|
#4
|
||||
|
||||
|
Quote:
Code:
pattern=$1
num=$2
shift
nawk -v pattern="$pattern" -v num="$num" '
{ x[NR % num] = $0 }
index( $0, pattern ) {
n = NR % num
top = n + num
while ( n++ < top ) print x[n % num]
}'
More recent versions on Solaris, include nawk and /usr/xpg4/bin/awk. The above version only handles strings, not regular expressions. The following one deals with regexps, but will duplicate lines if there is an overlap. (I may fix that if I have a moment or to, or perhaps someone else can do it.) Code:
pattern=$1
num=$2
shift
nawk -v num=$num '
{ x[NR % num] = $0 }
/'"$pattern"'/ {
n = NR % num
top = n + num
while ( n++ < top ) print x[n % num]
}'
|
||||
| Google The UNIX and Linux Forums |
| Tags |
| regex, regular expressions |
| Thread Tools | |
| Display Modes | |
|
|