Solved

grep lines , also showing above and below

Posted on 2006-10-20
8
1,353 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

777 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