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
Adding a word in front of a word of each line. Ramesh Vellanki Shell Programming and Scripting 4 07-02-2008 09:17 AM
query on how to search for a line and read 4th word from that line jaggesh UNIX for Dummies Questions & Answers 4 07-01-2008 10:21 PM
to get a line above the search word sachin.gangadha UNIX for Dummies Questions & Answers 16 01-07-2008 07:19 AM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 06:17 AM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 06:29 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 09-19-2008
Jeevan Salunke Jeevan Salunke is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 6
Question How to search word and get the line above it.

Hey all,

Could any one help me ...

I want to search the word and want the line above it.

eg. mytxt.log

My name is Jeevan.
240 is my address.

My name is Jhon.
390 is my address.
--------
i want to search "240" and want output as

My name is Jeevan.
240 is my address.

Meance i want both the line.

Please suggest some solution.
  #2 (permalink)  
Old 09-19-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,262
Code:
grep -B1 240 infile
  #3 (permalink)  
Old 09-19-2008
Jeevan Salunke Jeevan Salunke is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 6
Its not working

i have typed

grep -b1 PRICE temp.log

also tried

grep -B1 "PRICE" temp.log

it gives me following error...

grep: illegal option -- 1
Usage: grep -hblcnsviw pattern file . . .
  #4 (permalink)  
Old 09-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Not all grep versions have the -B option. Try with awk or sed instead.

Code:
sed -n '/240/{x;p;x;p;b;};h' file
If that doesn't work for you either, search the forums for "context grep".
  #5 (permalink)  
Old 09-19-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,082
Code:
awk '{
if($0 !~/^240/) 
t=$0
if($0 ~/^240/)
{
	print t
	print $0
}
}' filename
  #6 (permalink)  
Old 09-19-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Or perhaps more idiomatically

Code:
awk '/240/ { print t; print $0; next } { t = $0 }' filename
  #7 (permalink)  
Old 09-20-2008
Jeevan Salunke Jeevan Salunke is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 6
Wow...
Its working.
Thanks a lot for this.
one quick question how to save output of this awk in a variable.
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 05:12 AM.


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