Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-25-2012
Registered User
 
Join Date: Jun 2012
Posts: 23
Thanks: 11
Thanked 0 Times in 0 Posts
Display n lines before match found

I have a file with following data

A
B
C
D
E
F
G
H
I
K
L
M
N

and search pattern is G

Expected output

A
B
C
D
E
F

Note : Apart from grep -B i need some other options
Sponsored Links
    #2  
Old 06-25-2012
balajesuri's Avatar
#! /bin/bash
 
Join Date: Apr 2009
Location: India
Posts: 1,571
Thanks: 15
Thanked 441 Times in 426 Posts

Code:
awk '/G/{exit}'1 file

Sponsored Links
    #3  
Old 06-25-2012
bartus11's Avatar
Registered User
 
Join Date: Apr 2009
Posts: 3,146
Thanks: 3
Thanked 957 Times in 936 Posts

Code:
awk '/G/{p=1}!p' file

    #4  
Old 06-25-2012
alister alister is offline Forum Advisor  
Registered User
 
Join Date: Dec 2009
Posts: 2,603
Thanks: 123
Thanked 718 Times in 601 Posts

Code:
sed -n '/G/q;p'

Regards,
Alister
The Following User Says Thank You to alister For This Useful Post:
nsuresh316 (06-25-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk display the match and 2 lines after the match is found. eurouno UNIX for Dummies Questions & Answers 4 02-03-2012 08:16 PM
search and replace, when found, delete multiple lines, add new set of lines? DeuceLee Shell Programming and Scripting 3 11-23-2011 03:39 PM
Perl script to look for a string in a file and append few lines if the match is found arunkumarmc Programming 1 03-04-2011 05:05 PM
Grep and display n lines after the match is found. cv_pan UNIX for Dummies Questions & Answers 3 09-25-2008 11:15 PM
if match found go to a particular line in perl user_prady Shell Programming and Scripting 17 03-31-2008 03:00 PM



All times are GMT -4. The time now is 01:13 AM.