Script with awk and grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script with awk and grep
# 1  
Old 06-30-2011
Wrench Script with awk and grep

Hi .. I am working on a shell script to do following task. I have Input file as follows. I have to find failed or Offline disk and run command # hic -ip 172.124.24.59 getlogicalgrp |grep -B10 diskid against the disk ID which is 1 line above the disk state. I am using grep with -b10 is because Disk Group ID is usually within 10 lines and I don't know if there is better way to get the Disk Group ID. Then I run #hic -ip 172.124.24.59 diskgrp 261 -devlist command to get the devices impacted by failed/offline disk. I would really appreciate if you have better way to accomplish this.
Smilie

Input:
Code:
vBus 2 Target 0  Disk 7
State:                   Online

vBus 2 Target 0  Disk 8
State:                   Failed

vBus 2 Target 0  Disk 9
State:                   Offline

vBus 2 Target 0  Disk 10
State:                   Online

vBus 2 Target 0  Disk 11
State:                   Online

vBus 2 Target 0  Disk 12

Code:
 # hic -ip 172.124.24.59  getlogicalgrp  |grep -B10 diskid|grep DiskGroup| tail -n1 |awk '{print $NF}'

where diskid = "vBus 2 Target 0 Disk 7"

Code:
DiskGroup ID:                              261
List of disks:                        vBus 3 Target 4  Disk 10
                                           vBus 3 Target 4  Disk 11
                                           vBus 3 Target 4  Disk 12
                                           vBus 3 Target 4  Disk 13
                                           vBus 3 Target 4  Disk 14
                                           vBus 4 Target 3  Disk 10
                                           vBus 4 Target 3  Disk 11
                                           vBus 4 Target 3  Disk 12
                                           vBus 4 Target 3  Disk 13
                                           vBus 2 Target 0  Disk 7


#hic -ip 172.124.24.59   diskgrp 261 -devlist

# 2  
Old 06-30-2011
how about doing it with ex...
Code:
ex -s +"/Failed/-1 | /Offline/-1 | q!" file

# 3  
Old 06-30-2011
Code:
# ex -s +"/Failed/-1 p | /Offline/-1 p | q!" disk_tmp
-bash: !": event not found

---------- Post updated at 02:01 PM ---------- Previous update was at 01:55 PM ----------

Thanks shamrock for quick response. I tried your command but returns event not found.
# ex -s +"/Failed/-1 | /Offline/-1 | q!" disk_tmp
-bash: !": event not found
# 4  
Old 06-30-2011
Quote:
Originally Posted by dynamax
Code:
# ex -s +"/Failed/-1 p | /Offline/-1 p | q!" disk_tmp
-bash: !": event not found

That's your shell bungling that up. It assumes exclamation marks in quotes to be history references. If you put it in a script file, which provides no history functionality, it'd run fine.

Here's how you can modify it to avoid provoking BASH:
Code:
ex -s +"/Failed/-1 p | /Offline/-1 p | q"! disk_tmp

# 5  
Old 06-30-2011
Hi corona in k shell your code works fine but does not exit I have to type quit to exit.

Code:
# ex -s +"/Equalizing/-1 | /Offline/-1 | q!" disk_tmp

Bus 7 Enclosure 0  Disk 8





:q!

test
quir
quit

# 6  
Old 06-30-2011
As you are on bash try this one...
Code:
awk '/Failed|Offline/{print l}{l=$0}' file

# 7  
Old 06-30-2011
Thanks Shamrock ! That helps.

---------- Post updated at 02:33 PM ---------- Previous update was at 02:27 PM ----------

Is there any way I can capture the DiskGroup ID. i.e. if I grep for disk "vBus 2 Target 0 Disk 7" first occurrence of "DiskGroup ID" in the reverse order.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

2. Homework & Coursework Questions

Shell Script: Sorting by column using grep/awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: You will write a script that will read a tab-separated file that contains the names of all 50 states &... (7 Replies)
Discussion started by: tburns517
7 Replies

3. Shell Programming and Scripting

Using awk or grep with Variables in a script

Good day Geeks, Am having an issue with using variables in a rather simple script, the script is as follows: #!/bin/bash ### Script written by Adigun Gbenga ### Date: April 28, 2012 array=( 1 2 3 4 5 29 7 8 9... (6 Replies)
Discussion started by: infinitydon
6 Replies

4. Shell Programming and Scripting

AWK/GREP: grep only lines starting with integer

I have an input file 12.4 1.72849432773174e+01 -7.74784188610632e+01 12.5 9.59432114416327e-01 -7.87018212757537e+01 15.6 5.20139995965960e-01 -5.61612429666624e+01 29.3 3.76696387248366e+00 -7.42896194101892e+01 32.1 1.86899877018077e+01 -7.56508762501408e+01 35 6.98857157014640e+00... (2 Replies)
Discussion started by: chrisjorg
2 Replies

5. Shell Programming and Scripting

Help me with awk and grep script

#!/bin/sh PRINTF=/usr/bin/printf $PRINTF "Enter a UserID\n" read USERID for USERID in `cat passwd | awk -F : '{print $1}'` do USERHOME=`grep ^$/qianxin/ passwd | awk -F : '{print $6}'` USERSHELL=`grep ^$/qianxin/ passwd | awk -F : '{print $7}'` $PRINTF "$USERID\n" $PRINTF... (3 Replies)
Discussion started by: ikeQ
3 Replies

6. Shell Programming and Scripting

Shell script / Grep / Awk to variable and Loop

Hi, I have a text file with data in that I wish to extract, assign to a variable and process through a loop. Kind of the process that I am after: 1: Grep the text file for the values. Currently using: cat /root/test.txt | grep TESTING= | awk -F"=" '{ a = $2 } {print a}' | sort -u ... (0 Replies)
Discussion started by: Spoonless
0 Replies

7. Shell Programming and Scripting

AWK script for directory listing using GREP

Hi, All - script1.awk contains the following script: BEGIN { printf "The following are directory files:" "ls -l | grep '^d' | {print $0}" printf "There were "NR" directories in this list." } When I run the script, Here is what I get: $ awk -f... (4 Replies)
Discussion started by: ora_umair
4 Replies

8. Shell Programming and Scripting

MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else

Hi Guys, I need to set the value of $7 to zero in case $7 is NULL. I've tried the below command but doesn't work. Any ideas. thanks guys. MEM=`ps v $PPID| grep -i db2 | grep -v grep| awk '{ if ( $7 ~ " " ) { print 0 } else { print $7}}' ` Harby. (4 Replies)
Discussion started by: hariza
4 Replies

9. Shell Programming and Scripting

grep, awk, typeset in a shell script..

For e.g I have a file named "relation" which has three coloums i.e JOHN MARY JACK PETE ALISIA JONNY TONY JACKIE VICTOR If I do grep -w 'JOHN' relation | awk '{print""$1" is husband of "$2" & father of "$3""}' It gives out JOHN i husband of MARY & father of JACK (which is desired... (7 Replies)
Discussion started by: nick_25
7 Replies

10. Shell Programming and Scripting

script ksh/awk/grep

How can I extract the 9th line of a text file. thanks vm.:confused: (2 Replies)
Discussion started by: hoang
2 Replies
Login or Register to Ask a Question