![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| grep and display few lines before and after | melanie_pfefer | SUN Solaris | 8 | 09-25-2008 08:05 AM |
| grep -n lines before and after | airman_ole | Shell Programming and Scripting | 2 | 01-20-2008 10:33 PM |
| grep string & next n lines | ashterix | Shell Programming and Scripting | 8 | 11-21-2005 08:38 PM |
| Grep on multiple lines | gundu | Shell Programming and Scripting | 13 | 03-25-2005 11:43 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
Code:
metastat | sed -n '/Submirror/{p;n;p;}'
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
Both approaches are good, i guess i'll use this for almost the same operation, thanks for sharing this.
|
||||
| Google The UNIX and Linux Forums |