UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
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  
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: 5
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: 5
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: 5
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: 5
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,059 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: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
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: 5
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,059 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: 4 Days 3 h 46 m 33 sec
Reputation Power: 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

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: 5
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: 5
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: 5
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,059 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: 4 Days 3 h 46 m 33 sec
Reputation Power: 8
-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
Reply

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


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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