Solved

grep lines , also showing above and below

Posted on 2006-10-20
8
1,341 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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:bira
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
grep -A 1 -B 1 "two"
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 6

Accepted Solution

by:
bira earned 250 total points
Comment Utility
"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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
forget admin password 8 22
reinstall 1 55
Can I delete authorized_keys in FreeBSD 1 57
LINUX ZIP - UNCOMPRESS WINDOWS PATH 3 54
Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now