Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1389
  • Last Modified:

grep lines , also showing above and below

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
josephsonnd
Asked:
josephsonnd
  • 3
  • 2
  • 2
  • +1
1 Solution
 
biraCommented:
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
 
josephsonndAuthor Commented:
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
 
hughmCommented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sunnycoderCommented:
grep -A 1 -B 1 "two"
0
 
biraCommented:
"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
 
josephsonndAuthor Commented:
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
 
josephsonndAuthor Commented:
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
 
sunnycoderCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now