UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 11th, 2004, 05:06 AM
Vinod V Vinod V is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 36 Vinod V User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 10
Unhappy Displaying lines above grep-ed line

Hi,

How do i display 15 lines above the line that i grep for? Lets say i grep for "searchstring" in a flat file, i want to display 15 lines before the "searchstring" line. is ther a way to do this? Any help is appreciated. Thx in advance.

Reply With Quote
  #2  
Old August 11th, 2004, 07:54 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 10
There is a program called cgrep (for "context grep") that can do this. Here is a link. Go to that page and look for the cgrep links.

Also here is a perl implementation, and a shell script.

I've never used any of these, but I think they can do pretty much what you're looking for.

Reply With Quote
  #3  
Old August 12th, 2004, 02:14 AM
Vinod V Vinod V is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 36 Vinod V User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 10
Hi Perderabo,

I wish i could use the program, but i cant. This server i am working on is a production server and i can install any new programs on it .Is ther any other way i can do this?

Reply With Quote
  #4  
Old August 12th, 2004, 02:23 AM
Vinod V Vinod V is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 36 Vinod V User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 10
I was also wondering if i could display to the standard output cretain lines based on their line numbers. For example i could grep a line with its line number using the -n option then i could loop 15 lines before that and display those line. So lets say i grep the line at line number 42, i would -1 the line number and ouput the line at line 41, then -1 again and display line 40....and so on. Is there a way to display string based on specific line numbers? Thnx.

Reply With Quote
  #5  
Old August 12th, 2004, 03:08 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,200 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 1 Day 11 h 47 m 52 sec
Reputation Power: 12
you have a couple of probls
- almost unix tools, works sequentailly, so processing line x
they have no idea about line x-1
- what when before 'searchstring' there are NOT 15 lines ?
use awk, perl or c

read the file in an array
if you found 'searchstring' at line-number >14
print elements of array[pos-15,pos]
restart the job

Reply With Quote
  #6  
Old August 20th, 2004, 01:15 AM
Vinod V Vinod V is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 36 Vinod V User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 10
I finally managed to solve the line no problem. I grep-ed the line with the -n option and wrote the output to a file and grepped the line nos and did a while loop to read up and down of the grep-ed line. A bit slow but what the hell....it works

Reply With Quote
  #7  
Old August 21st, 2004, 12:48 PM
mtrxreloaded mtrxreloaded is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 1 mtrxreloaded User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
did u try grep -A (after) or grep -B (before) ?

Reply With Quote
  #8  
Old August 23rd, 2004, 09:21 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,200 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 1 Day 11 h 47 m 52 sec
Reputation Power: 12
a tricky but fast(er) way:
1) get line number (grep)
2) calculate -= START, if negative skipp it (bc)
3) generate a sed script (echo)
4) if successfull, exec it (sed)


Code:
#!/usr/bin/sh

FILE=afile
START=3
SEARCH=aaa

rm -f ${SED:=sedcmd}

(
   for num in `grep -n $SEARCH $FILE|sed 's/:.*//'`
   do
       start=`echo "s=$num-$START;if(s >= 0) s;"|bc`
       [ x$start = x ] && continue
       echo $start,${num}p
   done
) >$SED
[ -s $SED ] && sed -nf $SED $FILE
exit 0

Reply With Quote
  #9  
Old August 24th, 2004, 05:27 AM
Vinod V Vinod V is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 36 Vinod V User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 10
Hi guggach,

for some strange reason, it dosent display anything if there is only one occurance of the word being grep-ed. And if the word is sandwiched between othe chars, it is not being displayed either.

Reply With Quote
  #10  
Old August 24th, 2004, 05:32 AM
Vinod V Vinod V is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 36 Vinod V User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 10
hi mtrxreloaded,

I cant seem to find the -A or -B option for grep. Is that what you meant?

Reply With Quote
  #11  
Old August 24th, 2004, 05:37 AM
Vinod V Vinod V is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Posts: 36 Vinod V User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 m 20 sec
Reputation Power: 10
anyway.....this is my code
<code>
cat /tmp/vinod/linenos | while read LINE
do
lineno=$LINE
count=0
actline=0
lineend=`expr $lineno + 3`
linestr=`expr $lineno - 1`
actline=$lineno
echo >> results.txt
echo >> results.txt
while [ "$count" != "3" ]
do
actline=`expr $actline - 1`
grep $actline /tmp/vinod/searchtext >> /tmp/vinod/results.txt
count=`expr $count + 1`
if [ "$count" = "3" ];
then
grep $lineno /tmp/vinod/searchtext >> /tmp/vinod/results.txt
count=0
actline=$lineno
while [ "$count" != "3" ]
do
actline=`expr $actline + 1`
grep $actline /tmp/vinod/searchtext >> /tmp/vinod/results.txt
count=`expr $count + 1`
done
fi
echo >> /tmp/vinod/results.txt
echo >> /tmp/vinod/results.txt
done

done

</code>

Reply With Quote
  #12  
Old August 24th, 2004, 07:19 AM
guggach guggach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,200 guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level)guggach User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Week 1 Day 11 h 47 m 52 sec
Reputation Power: 12
-A -B is maybe gnu-grep.
you have solid shell misunderstandings

do you know the difference:
echo >> /tmp/vinod/results.txt
echo >> /tmp/vinod/results.txt

and:
(
echo
echo
) >/tmp/vinod/results.txt

put this in afile:

hallo
gugus
aaa
123
456
789
aaa
222
333
444
aaa
lollo
aaa

and try the script, it must print:

123
456
789
aaa
222
333
444
aaa

every thing else is cr...

Reply With Quote
  #13  
Old May 4th, 2009, 12:45 PM
microview microview is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 1 microview User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 48 sec
Reputation Power: 0
Quote:
Originally Posted by Vinod V
hi mtrxreloaded,

I cant seem to find the -A or -B option for grep. Is that what you meant?


GREP --help

Context control:
-B, --before-context=NUM print NUM lines of leading context
-A, --after-context=NUM print NUM lines of trailing context

exp: grep -A 4 "string"
- prints 4 lines after the matching string
exp: grep -B 4 "string"
- prints 4 lines before the matching string

Reply With Quote
  #14  
Old May 31st, 2011, 11:46 AM
Aamir814 Aamir814 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2011
Posts: 1 Aamir814 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 m 26 sec
Reputation Power: 0
One line command to display 5 lines above and 5 lines below the grep-ed line:

grep -n "<pattern>" testfile.txt | awk -F":" '{print $1-5 "," $1+5 "p"}' | xargs -t -i sed -n {} testfile.txt

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Displaying lines above grep-ed line

Developer Shed Advertisers and Affiliates



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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap