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
Cutting specific lines from a file pathanjalireddy Shell Programming and Scripting 15 06-04-2009 02:32 AM
overwrite specific lines in a file csecnarf Shell Programming and Scripting 7 05-13-2008 12:01 PM
how to remove specific lines from a file bluemoon1 Shell Programming and Scripting 17 10-07-2007 11:40 PM
replacing specific lines in a file hcclnoodles Shell Programming and Scripting 6 09-30-2006 12:35 PM
How do you specific lines in a file? hedgehog001 UNIX for Dummies Questions & Answers 2 08-23-2005 01:04 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-22-2008
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
retrieving specific lines from a file - can I use grep ?

Hi there, if i had a file that looked like this


Code:
my_server1
   red
   green
   blue
   yellow
   blue

my_server2 

  blue
  blue
  yellow
  green
  blue

my_server3

  yellow
  yellow
  blue

and I wanted to be able to grep for the hostname line and every instance of , say the word blue that appear below that hostname (but above the next hostname) ...what tool could i use ...it seems to me that grep doesnt seem to have the ability to be as specific as I require ...is there anything else ?

I want to get an output like this


Code:
my_server1
   blue
   blue
my_server2 
  blue
  blue
  blue
my_server3
  blue


any help on this would be great
  #2 (permalink)  
Old 09-22-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,440

Code:
grep 'my_server\|blue' file

  #3 (permalink)  
Old 09-22-2008
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
Quote:
Originally Posted by danmero View Post
Code:
grep 'my_server\|blue' file
OK, forgot to mention im running on Solaris, the above command returns nothing at all
  #4 (permalink)  
Old 09-22-2008
manosubsulo manosubsulo is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 87

Code:
grep 'blue' my_server* | awk -F: '$1!=p1{print $1;} {print $2;p1=$1;p2=$2}'

  #5 (permalink)  
Old 09-22-2008
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
thanks for the reply, it didnt work so i removed the last bit, the awk statement (see below) and even the grep on its own is not returning anything

Code:
cat file | grep 'blue' my_server*


to be honest im a bit lost with this one
  #6 (permalink)  
Old 09-22-2008
danmero danmero is offline Forum Advisor  
  
 

Join Date: Nov 2007
Location: 45.48-73.63
Posts: 1,440
I give you 3 options here:

Code:
grep -e my_server -e blue file
awk '$0 ~ "my_server|blue"' file
sed '/myserver\|blue/p;d' file

  #7 (permalink)  
Old 09-22-2008
hcclnoodles hcclnoodles is offline
Registered User
  
 

Join Date: Mar 2002
Posts: 272
Thankyou very much for taking the effort to give me those three options..I have tried them all with no success though ... I thank you anyway



Code:
# grep -e my_server -e blue /opt/my_file
grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . .

# awk '$0 ~ "my_server|blue"' /opt/my_file
awk: syntax error near line 1
awk: bailing out near line 1

# sed '/my_server\|blue/p;d' /opt/my_file
#     <<< no output

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:06 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