LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   print value from offset row with bash (https://www.linuxquestions.org/questions/linux-newbie-8/print-value-from-offset-row-with-bash-4175504094/)

mightyh 05-06-2014 12:15 PM

print value from offset row with bash
 
I have a command that provides system config info for each volume. I am trying to query a file containing the information for each volume, search out a volume group and respond with the associated volume. So basically

if the associated volume group = x then print volume name.

Code:

Volume name:                  2

Volume status:                Optimal
Thin provisioned:             

Capacity:                      ##### GB
Volume world-wide identifier: 
Subsystem ID (SSID):         
Associated volume group:      4
RAID level:                   

LUN:                         
Accessible By:               

Drive media type:             
Drive interface type:         
Tray loss protection:         

Secure:                       

Data Assurance (DA) enabled: 

Preferred owner:             
Current owner:               



Volume name:                  5

Volume status:                Optimal
Thin provisioned:             

Capacity:                      ##### GB
Volume world-wide identifier: 
Subsystem ID (SSID):         
Associated volume group:      5
RAID level:                   

LUN:                         
Accessible By:               

Drive media type:             
Drive interface type:         
Tray loss protection:         

Secure:                       

Data Assurance (DA) enabled: 

Preferred owner:             
Current owner:


grail 05-06-2014 01:51 PM

Sounds like a great idea ... so what have you done to solve the problem? Also, is the data shown the answer or what we are to start with?

Please also place code / data in [code][/code] tags to make it more readable.

mightyh 05-06-2014 03:14 PM

That is a sample of the data that i am trying to parse. I will turn the solution into a variable "$volumename" for another function.

I have a basic understanding of awk so i can definitely find the line with associated volume group but i am at a loss on how to print the volume name from 8 rows up.

This will find the line with volumegroup 1 and print it. But what i need is to print the volume name.
Code:

volumegroup = 1
awk -v volumegroup=$volumegroup -F":" '$1 ~ /volume group/ && $2 ~ volumegroup {print}'


grail 05-07-2014 07:29 AM

So why not store the volume name as you come across them and then when your expressions are correct you can print the saved value

mightyh 05-08-2014 05:36 PM

I would be grateful if you would show me how to do that, or point me to an existing thread.

grail 05-09-2014 02:51 AM

Same idea as what you have already done, simply add something like the following to the start:
Code:

$1 = "Volume name"{vname = $2}
To get just the name, seeing as there is a good deal of white space everywhere, I would add this to your separator so it is absorbed:
Code:

-F": *"
If there are tabs in there as well you will need to alter to suit


All times are GMT -5. The time now is 08:45 PM.