BASH: extracting values from multiple lines after a match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH: extracting values from multiple lines after a match
# 1  
Old 12-17-2010
BASH: extracting values from multiple lines after a match

Hi there, I have the following output,

Code:
# raidctl -l
RAID    Volume  RAID            RAID            Disk
Volume  Type    Status          Disk            Status
------------------------------------------------------
c0t1d0  IM      OK              c0t1d0          OK
                                c0t3d0          OK
c0t4d0  IM      OK              c0t4d0          OK
                                c0t6d0          OK
                                c0t5d0          OK
                                c0t7d0          OK
c0t0d0  IM      OK              c0t2d0          OK
                                c0t0d0          OK

I need to extract a comma delimited list (or maybe a BASH array) of the disks that happen to be part of a particular RAID Volume. So for example

If i was querying for a "RAID Volume" (first column) that contained "c0t4d0" then i would want to receive either an array with the following elements (from the fourth column)

c0t4d0
c0t6d0
c0t5d0
c0t7d0


or a comma delimited list like this (all of which is for further processing)

c0t4d0,c0t6d0,c0t5d0,c0t7d0

Likewise, whatever "RAID Volume" I query would always return its member disks. NOTE: this can be a variable number as illustrated in the example above

Could anybody point me in the right direction on how i could do this? Any help would be greatly appreciated
# 2  
Old 12-17-2010
Code:
$> awk '$1 ~ /^c0t4d0/ {print $4; x=1; next} x==1 && NF==2 {print $1} x==1 && NF>2 {x=0}' infile
c0t4d0
c0t6d0
c0t5d0
c0t7d0

#or

$> awk '$1 ~ /^c0t4d0/ {l=$4; x=1; next} x==1 && NF==2 {l=l","$1} x==1 && NF>2 {x=0} END{print l}' infile
c0t4d0,c0t6d0,c0t5d0,c0t7d0

# 3  
Old 12-17-2010
thanks Zaxxon for replying, however if you run that against the bottom most "RAID Volume" called "c0t0d0" it only comes back with the following (note the blank line at the end)

Code:
# raidctl -l | awk '$1 ~ /^c0t0d0/ {print $4; x=1; next} x==1 && NF==2 {print $1} x==1 && NF>2 {x=0}'
c0t2d0

#

It only seems to work fine if the "RAID Volume" matches the same disk on the SAME line. if I edit the infile to look like this (i.e. swap the $4 numbers so that they dont match the RAID Volume.

Code:
# raidctl -l
RAID    Volume  RAID            RAID            Disk
Volume  Type    Status          Disk            Status
------------------------------------------------------
c0t1d0  IM      OK      c0t3d0          OK
                        c0t1d0          OK
c0t4d0  IM      OK      c0t6d0          OK
                        c0t4d0          OK
                        c0t5d0          OK
                        c0t7d0          OK
c0t0d0  IM      OK      c0t2d0          OK
                        c0t0d0          OK

then the output looks like this

Code:
# awk '$1 ~ /^c0t4d0/ {print $4; x=1; next} x==1 && NF==2 {print $1} x==1 && NF>2 {x=0}' /tmp/ra
c0t6d0

c0t5d0
c0t7d0
#


Also, another question if I may, is it easy for me to place a variable in the command as opposed to the actual string?, when i do this, it returns no result. Let me illustrate
Code:
# awk '$1 ~ /^c0t4d0/ {print $4; x=1; next} x==1 && NF==2 {print $1} x==1 && NF>2 {x=0}' infile
c0t4d0
c0t6d0
c0t5d0
c0t7d0

# DISK=c0t4d0

# echo $DISK
c0t4d0

# awk '$1 ~ /^${DISK}/ {print $4; x=1; next} x==1 && NF==2 {print $1} x==1 && NF>2 {x=0}' infile
#

I've tried escaping the variable and using double quotes but no joy

again thank you for helping with this Smilie
# 4  
Old 12-17-2010
Oops good point:
Code:
$> awk '$1 ~ /c0t0d0/ && NF>2 {print $4; x=1; next} x==1 && NF==2 {print $1} x==1 && NF>2 {x=0}' infile
c0t2d0
c0t0d0

The bold has been added - it should work now in the posted circumstances.

For the input to be passed you could do:
Code:
$> DISK=c0t1d0
$> awk -v disk=$DISK '$1 == disk && NF>2 {print $4; x=1; next} x==1 && NF==2 {print $1} x==1 && NF>2 {x=0}' infile
c0t1d0
c0t3d0

# 5  
Old 12-20-2010
thank you very much zaxxon, that works fantaistically, can i just confrim this is how it works

a) if the first column ($1) is equal to $disk and the number of columns > 2 then print the 4th column, set x to 1 and go to the next line?

b) on the next line, if x equals 1 and number of columns equals 2 then print the first visible column

c) on the next line, if x eq 1 and the number of fields > than 2, then set x back to 0 again.


A quick question if i may, why is there no "next" command on the second if statement, does it automatically go to the next line anyway, if so, why did we use it before ..

also, does it process the whole awk statement sequentially for every single line? for example, on the third if statement, we process a line and dont do anything with it other than setting x back to 0. will this line get processed again properly in due course ?

thank you again for help on this

Cheers
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

Awk match multiple columns in multiple lines in single file

Hi, Input 7488 7389 chr1.fa chr1.fa 3546 9887 chr5.fa chr9.fa 7387 7898 chrX.fa chr3.fa 7488 7389 chr21.fa chr3.fa 7488 7389 chr1.fa chr1.fa 3546 9887 chr9.fa chr5.fa 7898 7387 chrX.fa chr3.fa Desired Output 7488 7389 chr1.fa chr1.fa 2 3546 9887 chr5.fa chr9.fa 2... (2 Replies)
Discussion started by: jacobs.smith
2 Replies

5. Shell Programming and Scripting

AWK script - extracting min and max values from selected lines

Hi guys! I'm new to scripting and I need to write a script in awk. Here is example of file on which I'm working ATOM 4688 HG1 PRO A 322 18.080 59.680 137.020 1.00 0.00 ATOM 4689 HG2 PRO A 322 18.850 61.220 137.010 1.00 0.00 ATOM 4690 CD ... (18 Replies)
Discussion started by: grincz
18 Replies

6. UNIX for Dummies Questions & Answers

extracting lates pattern match from multiple matches in log

Hi, I have a large, multiline log file. I have used pcregrep to extract all entries in that log that match a particular pattern - where that pattern spans multiple lines. However, because the log file is large, and these entries occur every few minutes, I still output a very large amount... (6 Replies)
Discussion started by: dbrb2
6 Replies

7. Shell Programming and Scripting

Extracting N lines match number X of a pattern

Hi All, as the title says I need to extract N lines after match number X of a pattern. e.g. 111 xxx xxx 111 yyy yyy 111 www www 111 zzz zzz I would like to extract the two lines after the second 111 occurrence. I tried with grep but I didn't find any options to do that. Any... (11 Replies)
Discussion started by: f_o_555
11 Replies

8. UNIX for Dummies Questions & Answers

Extracting m lines after n lines after match

Hi All, I would like to extract from a text file m lines skipping n lines after a string occurrency. Is it possible with grep? e.g. qqq ww eee rrr ttt yyy uuu I want to print 2 lines skipping 1 line after the string 'ww' result would be rrr ttt (2 Replies)
Discussion started by: f_o_555
2 Replies

9. Shell Programming and Scripting

help extracting a matching pattern and next lines of match

Hi there, i'm having some problems just making an awk script (i've tried this way, but other way can be posible for sure), for the next file file.txt <register> <createProfile> <result>0</result> <description><!]></description> <msisdn>34661461174</msisdn> <inputOmvID>1</inputOmvID>... (6 Replies)
Discussion started by: vicious
6 Replies

10. UNIX for Dummies Questions & Answers

Extracting lines that match string at certain position

I have a fixed length file in the following format <date><product_code><other data> The file size is huge and I have to extract only the lines that match a certain product code which is of 2 bytes length. I cannot use normal grep since that may give undesirable results. When I search for prod... (5 Replies)
Discussion started by: paruthiveeran
5 Replies
Login or Register to Ask a Question