The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Operating Systems
> UNIX Help
|
Displaying lines above grep-ed line
Discuss Displaying lines above grep-ed line in the UNIX Help forum on Dev Shed. Displaying lines above grep-ed line UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

August 11th, 2004, 05:06 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 36
Time spent in forums: 5 m 20 sec
Reputation Power: 10
|
|
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.
|

August 11th, 2004, 07:54 PM
|
|
Contributing User
|
|
Join Date: Nov 2003
Posts: 121
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.
|

August 12th, 2004, 02:14 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 36
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?
|

August 12th, 2004, 02:23 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 36
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.
|

August 12th, 2004, 03:08 AM
|
|
Contributing User
|
|
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,200
  
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

|

August 20th, 2004, 01:15 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 36
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 
|

August 21st, 2004, 12:48 PM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
did u try grep -A (after) or grep -B (before) ?
|

August 23rd, 2004, 09:21 AM
|
|
Contributing User
|
|
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,200
  
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
|

August 24th, 2004, 05:27 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 36
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.
|

August 24th, 2004, 05:32 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 36
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?
|

August 24th, 2004, 05:37 AM
|
|
Contributing User
|
|
Join Date: Apr 2004
Posts: 36
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>
|

August 24th, 2004, 07:19 AM
|
|
Contributing User
|
|
Join Date: Jul 2004
Location: Middle Europa
Posts: 1,200
  
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...
|

May 4th, 2009, 12:45 PM
|
|
Registered User
|
|
Join Date: May 2009
Posts: 1
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
|

May 31st, 2011, 11:46 AM
|
|
Registered User
|
|
Join Date: May 2011
Posts: 1
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|