|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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? |
|
#4
|
|||
|
|||
|
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.
|
|
#5
|
|||
|
|||
|
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 ![]() |
|
#6
|
|||
|
|||
|
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
![]() |
|
#7
|
|||
|
|||
|
did u try grep -A (after) or grep -B (before) ?
|
|
#8
|
|||
|
|||
|
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
|
|
#9
|
|||
|
|||
|
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. |
|
#10
|
|||
|
|||
|
hi mtrxreloaded,
I cant seem to find the -A or -B option for grep. Is that what you meant? |
|
#11
|
|||
|
|||
|
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> |
|
#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... |
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Displaying lines above grep-ed line |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|