Solved

grep lines , also showing above and below

Posted on 2006-10-20
8
1,349 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
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17773653
grep -A 1 -B 1 "two"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AIX print queues constantly going down 11 490
Red Hat Enterprise Linux 6.5 gets missing "/lib/ld-linux.so.2" 7 71
aix tls version 6 223
Install MySQL 5.6 and PHP on Centos Linux 6 101
Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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.

895 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

13 Experts available now in Live!

Get 1:1 Help Now