Solved

grep lines , also showing above and below

Posted on 2006-10-20
8
1,364 Views
Last Modified: 2008-02-01
I use grep a lot, works fine

Is there a way to show the current , one line above and one line below
Ex:
$cat test
This is line one
This is line two
This is line three

grep two test                 # This will show as follows
This is line one
This is line two
This is line three

0
Comment
Question by:josephsonnd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:bira
ID: 17773500
a=`fgrep -n two test|cut -f1 -d':'`
line_before=`expr $a \- 1`
line_after=`expr $a \+ 1`
cat test|head -$line_before|tail -1
grep two test
cat test|head -$line_after|tail -1
0
 

Author Comment

by:josephsonnd
ID: 17773571
bira,
Thanks
The idea is very good
If I want to make a generic search on a large file (say 100 lines), for a occurance of a string , and out put should show each occurance along with the previous line and the next line, how will I do it
0
 

Expert Comment

by:hughm
ID: 17773580
i am sure someone will have a more elegant method... ;-)

typeset -i actline lines startline

lines=`cat test | wc -l`
actline=`grep -n "two" test | cut -f1 -d:`      
startline=$lines-$actline+2
cat test | tail -$startline | head -3
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 45

Expert Comment

by:sunnycoder
ID: 17773653
grep -A 1 -B 1 "two"
0
 
LVL 6

Accepted Solution

by:
bira earned 250 total points
ID: 17773744
"If I want to make a generic search on a large file (say 100 lines), for a occurance of a string , and out put should show each occurance along with the previous line and the next line, how will I do it"

this should do the work:

for i in `fgrep -n two test|cut -f1 -d':'`
do
line_before=`expr $i \- 1`
line_after=`expr $i \+ 1`
cat test|head -$line_before|tail -1
cat test|head -$i|tail -1
line_before=`expr $i \- 1`
cat test|head -$line_after|tail -1
echo "----------------------------------------------"
done
0
 

Author Comment

by:josephsonnd
ID: 17774222
bira,

This works very well, Thanks.
I modified to print 2 lines above and 1 line below
Feel free to make any comments

for i in `fgrep -n two test|cut -f1 -d':'`
do
line_before1=`expr $i \- 1`
line_before2=`expr $i \- 2`
line_after=`expr $i \+ 1`
cat test|head -$line_before2|tail -1
cat test|head -$line_before1|tail -1
cat test|head -$i|tail -1
cat test|head -$line_after|tail -1
echo "----------------------------------------------"
done
0
 

Author Comment

by:josephsonnd
ID: 17774234
sunnycoder,

I get the following error. Any special setting needed

$ grep -A 1 -B 1 "two" test
grep: illegal option -- A
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] -e pattern_list...
        [-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] [-e pattern_list...]
        -f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-bhinsvx] pattern [file...]
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17774628
Your version of grep does not support -A and -B options
 grep -C 1 "two"
would have had the same effect.

Install gnu grep if possible .. would make this task much easier
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question