Visit The New, Modern Unix Linux Community


Print the above and below lines for the grep pattern.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print the above and below lines for the grep pattern.
# 1  
Print the above and below lines for the grep pattern.

Hi,

i would like to get the above and below lines of the grep pattern .
For ex :
file as below:
chk1- aaaa
1-Nov
chk2 -aaaa
##########
chk1-bbbbbb
1-Nov
chk2-bbbbbb
#########

my search pattern is date : 1-Nov


i need the o/p as below
chk1- aaaa
1-Nov
chk2 -aaaa
chk1-bbbbbb
1-Nov
chk2-bbbbbb

Thanks in Advance Smilie
# 2  
Code:
$ grep -B1 -A1 1-Nov inputfile
chk1- aaaa
1-Nov
chk2 -aaaa
--
chk1-bbbbbb
1-Nov
chk2-bbbbbb

# 3  
If your grep doesn't support that option, with awk...
Code:
 
awk '/1-Nov/{print prev"\n"$0;getline;print}{prev=$0}' infile

# 4  
Try this,
Code:
awk '{if(/1-Nov/){print a"\n"$0;getline;print $0} else {a=$0}}' inputfile

# 5  
got error :

illegal option -A
illegal option -1

---------- Post updated at 11:49 AM ---------- Previous update was at 11:38 AM ----------

its working Smilie .

if i want the below two lines ..
kindly help
# 6  
Quote:

its working Smilie .

if i want the below two lines ..
kindly help
Code:
awk '/1-Nov/{print prev"\n"$0;getline;print;getline;print}{prev=$0}' infile

# 7  
Code:
grep -C 1 "string" file

To get the same behaviour as grep -A/B/C we need to check if the line exists before printing it:
Code:
awk '/string/{if(p)print p;print;if(getline)print}{p=$0}' infile

or
Code:
sed -n '/string/{1!x;1!G;$!N;p;};h' infile

Otherwise there is an extra newline or a double last line...

Last edited by Scrutinizer; 11-01-2010 at 04:15 AM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #361
Difficulty: Medium
The rmdir command remove directories, even if they are not empty.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. UNIX for Dummies Questions & Answers

Grep -v lines starting with pattern 1 and not matching pattern 2

Hi all! Thanks for taking the time to view this! I want to grep out all lines of a file that starts with pattern 1 but also does not match with the second pattern. Example: Drink a soda Eat a banana Eat multiple bananas Drink an apple juice Eat an apple Eat multiple apples I... (8 Replies)
Discussion started by: demmel
8 Replies

3. AIX

Grep a pattern and print following n lines

Hi all, I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing. I have used the below code grep -A 3 "pattern" filename But it is throwing error as below. grep: illegal option -- A Can... (14 Replies)
Discussion started by: ssk250
14 Replies

4. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

5. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

6. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

7. Shell Programming and Scripting

print lines up to pattern excluding pattern

11 22 33 44 55 66 77 When pattern 55 is met, print upto it, so output is 11 22 33 44 (1 Reply)
Discussion started by: anilcliff
1 Replies

8. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

9. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

10. Shell Programming and Scripting

print certain pattern from 2 lines

i have 2 lines comming out of a script o/p.below the line. 2008-10-14 05:47:05,551 INFO - LPBatch: 2008-10-14 05:47:05,575 INFO - Number of Intervals Not Inserted: 1 / 95 -------------------------------------------------------------------------- How to print the below o/p from the... (2 Replies)
Discussion started by: ali560045
2 Replies

Featured Tech Videos