The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to print the lines after 2nd line from text file ram1729 Shell Programming and Scripting 6 09-16-2008 10:34 PM
reverse search a text file from a specified line PacificWonder Shell Programming and Scripting 14 08-28-2008 04:28 PM
need to search text and output previous lines grinds Shell Programming and Scripting 8 05-07-2008 09:31 AM
how to print out line numbers of a text file? forevercalz Shell Programming and Scripting 4 12-12-2005 05:04 AM
Search for text and print the next line alijassim Shell Programming and Scripting 2 08-17-2004 06:08 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-02-2009
kamranjalal kamranjalal is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 9
Search text from a file and print text and one previous line too

Hi,

Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11

Thanks for your help.
  #2 (permalink)  
Old 01-02-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
One way:

Code:
awk '/pattern/{print s "\n" $0;exit}{s=$0}' file
Regards
  #3 (permalink)  
Old 01-02-2009
Rhije Rhije is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 103
I have no idea what HP-UX 11.11 is.. but normal *nix shell, you can do:

grep -A2 "sometext" somefile.ext

It would grab that text, and the two lines afterwards.. You can also use -B for before.
  #4 (permalink)  
Old 01-02-2009
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Or:

Code:
grep -B1 pattern file
or:

Code:
sed -n -e '/pattern/{x;p;x;p}' -e h file
  #5 (permalink)  
Old 01-02-2009
kamranjalal kamranjalal is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 9
Please "cfajohnson" What should I change in the following command to find text and its previous line.

I got command (text=start; awk '/'"$text"'/{n=2}n-->0' filename) for finding text and next line.


Thanks for your help.
  #6 (permalink)  
Old 01-05-2009
kamranjalal kamranjalal is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 9
Hi,

Command awk '/pattern/{print s "\n" $0;exit}{s=$0}' file only returns the first occurance of the pattern, it is not showing the next occurance.

Please help me, thanks
  #7 (permalink)  
Old 01-06-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Remove the exit command:

Code:
awk '/pattern/{print s "\n" $0}{s=$0}' file
Regards
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 12:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0