The UNIX and Linux Forums  


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
grep and display few lines before and after melanie_pfefer SUN Solaris 13 02-19-2009 11:54 PM
Grep and display n lines after the match is found. cv_pan UNIX for Dummies Questions & Answers 3 09-26-2008 12:15 AM
Display file without # lines dave123 Shell Programming and Scripting 5 06-28-2008 01:22 PM
Display lines of the file on a log sbryant Shell Programming and Scripting 2 02-26-2008 02:16 PM
display few lines of the file sharuvman UNIX for Dummies Questions & Answers 4 04-27-2004 08:14 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-25-2009
user7617 user7617 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
grep and display lines from a file

I have to grep on a few words in a file and then display the line containing those words and the line above it.

For ex -

File1.txt contains...

abc xyz abc
This is a test
Test successful
abc xyz abc
Just a test
Test successful


I find the words 'Test successful' in the file and then I have to display the line containing these words and the line above it.

So my output should be...

This is a test
Test successful
Just a test
Test successful

I am just not sure how to display the line above the 'Test successful' in my result.
  #2 (permalink)  
Old 08-25-2009
cabrao cabrao is offline
Registered User
  
 

Join Date: Aug 2009
Posts: 40
grep -B1 "Test successful" yourfile
  #3 (permalink)  
Old 08-25-2009
aster007 aster007 is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 11
var=`grep -n "Test successful" <filename> | cut -f1 -d ":"`

for i in $var
do
head -$i <filename> | tail -2
done
  #4 (permalink)  
Old 08-25-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,400
use this awk utility

Code:
awk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=1 a=0 s="Test successful" filename

where a=no of lines below the pattern
b=no of lines above the pattern
s=pattern
  #5 (permalink)  
Old 08-25-2009
user7617 user7617 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
aster007,

that worked very well...

Can you also let me know how can I get the same result from all files in the directory (using '*') rather than putting <filename>?
  #6 (permalink)  
Old 08-25-2009
peterro peterro is offline
Registered User
  
 

Join Date: Jul 2009
Location: Gresham, OR
Posts: 153

Code:
for file in *
do
  var=`grep -n "Test successful" $file | cut -f1 -d ":"`
  for i in $var
  do
    head -$i $file | tail -2
  done
done

  #7 (permalink)  
Old 08-28-2009
user7617 user7617 is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 3
That worked awsome!

Thank you -- Peterro and aster007.
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 10:07 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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