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
grep -n lines before and after airman_ole Shell Programming and Scripting 5 07-05-2009 10:57 PM
grep and display few lines before and after melanie_pfefer SUN Solaris 13 02-19-2009 11:54 PM
grep string & next n lines ashterix Shell Programming and Scripting 8 11-21-2005 11:38 PM
Grep on multiple lines gundu Shell Programming and Scripting 13 03-25-2005 02:43 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-02-2006
Janus Janus is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 18
grep + lines after

Hello again, I'm still working on the redo of my script I created before and came across a grep question.

I'm on SunOS 5.9 and using the Korn Shell. I'm writing a function to check for validation of root disks being mirrored. If the server allows me to use metastat, I'm looking to show it has root disks, it has mirrors, and that the status is "okay". As always when I post, I give a little to take a little.

My Offering, the first 2/3rds of my Metastat Mirror Check Function:

#!/bin/ksh
#set -x
mirnumvar=`metastat | grep "^d" | grep "Mirror" | cut -d: -f1`
for varmirnum in $mirnumvar
do
#echo $varmirnum
mirvar=`metastat | grep "^d" | grep "Mirror" | grep $varmirnum | cut -d: -f1 | cut -dd -f2`
#echo $mirvar
submirvar=`metastat | grep "^d" | grep "d.$mirvar" | cut -d: -f1`
#echo $submirvar
echo "Mirror $varmirnum Contains Submirrors:" $submirvar
done

This gives output of:
Mirror d4 Contains Submirrors: d14 d24
Mirror d3 Contains Submirrors: d13 d23
Mirror d1 Contains Submirrors: d11 d21
Mirror d0 Contains Submirrors: d10 d20
Mirror d5 Contains Submirrors: d15 d25

I'm now working on grouping together statuses with their respective submirrors. One of the commands I've tinkered with that has gotten me close to the results is the following:

metastat | egrep 'Submirror of|State:'

Results:

State: Okay
State: Okay
d14: Submirror of d4
State: Okay
d24: Submirror of d4
State: Okay

State: Okay
State: Okay
d13: Submirror of d3
State: Okay
d23: Submirror of d3
State: Okay

State: Okay
State: Okay
d11: Submirror of d1
State: Okay
d21: Submirror of d1
State: Okay

State: Okay
State: Okay
d10: Submirror of d0
State: Okay
d20: Submirror of d0
State: Okay

State: Okay
State: Okay
d15: Submirror of d5
State: Okay
d25: Submirror of d5
State: Okay


I have bolded what my desired results would contain, the submirrors and it's statuses underneath it. The other State: Okay's are from the Main root disk Mirrors. Would I be working with properties of grep to try and grep the line after the submirror or a different command to tackle this scenario. As always, hats off and many thanks to those that can tackle this conundrum.

~Ryan
  #2 (permalink)  
Old 11-03-2006
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
metastat | sed -n '/Submirror/{p;n;p;}'
  #3 (permalink)  
Old 11-03-2006
Janus Janus is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 18
Submirror 0: d14
State: Okay
Submirror 1: d24
State: Okay
d14: Submirror of d4
State: Okay
d24: Submirror of d4
State: Okay
Submirror 0: d13
State: Okay
Submirror 1: d23
State: Okay
d13: Submirror of d3
State: Okay
d23: Submirror of d3
State: Okay
Submirror 0: d11
State: Okay
Submirror 1: d21
State: Okay
d11: Submirror of d1
State: Okay
d21: Submirror of d1
State: Okay
Submirror 0: d10
State: Okay
Submirror 1: d20
State: Okay
d10: Submirror of d0
State: Okay
d20: Submirror of d0
State: Okay
Submirror 0: d15
State: Okay
Submirror 1: d25
State: Okay
d15: Submirror of d5
State: Okay
d25: Submirror of d5
State: Okay

This is the result of the sed, again it's a close try but not what I was looking to get exactly. This is the desired output:

d14: Submirror of d4
State: Okay
d24: Submirror of d4
State: Okay
d13: Submirror of d3
State: Okay
d23: Submirror of d3
State: Okay
d11: Submirror of d1
State: Okay
d21: Submirror of d1
State: Okay
d10: Submirror of d0
State: Okay
d20: Submirror of d0
State: Okay
d15: Submirror of d5
State: Okay
d25: Submirror of d5
State: Okay

I appreciate the efforts though! I'm going to keep trying today at work too, thanks again everyone!
  #4 (permalink)  
Old 11-03-2006
Janus Janus is offline
Registered User
  
 

Join Date: Nov 2006
Posts: 18
Worked on it with my Unix Mentor and wanted to share the results, vgersh, you were real close!

2 ways to approach with the "sed" command:

Approach 1: metastat | sed -n '/Mirror/{n;p;n;p;n;p;n;p;}'
Results:
Submirror 0: d14
State: Okay
Submirror 1: d24
State: Okay
Submirror 0: d13
State: Okay
Submirror 1: d23
State: Okay
Submirror 0: d11
State: Okay
Submirror 1: d21
State: Okay
Submirror 0: d10
State: Okay
Submirror 1: d20
State: Okay
Submirror 0: d15
State: Okay
Submirror 1: d25
State: Okay

Which is probably what i'll use to work with for the script, but there's also

Approach 2: metastat | sed -n '/Mirror/{p;n;p;n;p;n;p;n;p;}'
Results:
d4: Mirror
Submirror 0: d14
State: Okay
Submirror 1: d24
State: Okay
d3: Mirror
Submirror 0: d13
State: Okay
Submirror 1: d23
State: Okay
d1: Mirror
Submirror 0: d11
State: Okay
Submirror 1: d21
State: Okay
d0: Mirror
Submirror 0: d10
State: Okay
Submirror 1: d20
State: Okay
d5: Mirror
Submirror 0: d15
State: Okay
Submirror 1: d25
State: Okay

Either way works, but there you go! Thanks again for all the effort and help!
~Ryan
  #5 (permalink)  
Old 11-03-2006
sysgate's Avatar
sysgate sysgate is offline Forum Advisor  
Unix based
  
 

Join Date: Nov 2006
Location: Bulgaria
Posts: 1,318
Both approaches are good, i guess i'll use this for almost the same operation, thanks for sharing this.
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 02:52 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