Skip to main content
added 767 characters in body
Source Link
chaos
  • 49.4k
  • 11
  • 128
  • 147

With awk:

awk 'NR==1{for(i=1;i<NF;i++){if($i=="OS_NATIVE_NAME"){s=index($0,$i); l=index($0,$(i+1))-s}}} $1=="disk_0"{print substr($0,s,l)}' file 
  • NR==1 if it's the first line (the header line), NR is awks internal variable for the current line number being processed.
  • for(i=1;i<NF;i++) loop trough the fields. NF is awks internal variable for the number of fields in the current line.
  • $i=="OS_NATIVE_NAME" since, we're looping trough each field, check if the field value equals OS_NATIVE_NAME.
  • s=index(...) find the position of the start of the field and save it for later. index() is awks string function to get the position of the occurence of a string (here the value of $i, hence OS_NATIVE_NAME) in another string (here $0, hence the whole line).
  • l=index(...)-s andget the length of the field and save it for later: same priciple as before, but to get the length we must substract s from it.
  • $1=="disk_0" find the DEVICE you are searching in the first field (disk_0 in the example). $1 represents the first field.
  • {print substr($0,s,l)} finally print for each line, the string started at position s, with length l. substr() is awks string function to cut a string (here $0; the whole line) from position s with length l (the two varibale we prevously extracted while processing line 1)

Prints (regardless of where the field is in the input):

sda 

With awk:

awk 'NR==1{for(i=1;i<NF;i++){if($i=="OS_NATIVE_NAME"){s=index($0,$i); l=index($0,$(i+1))-s}}} $1=="disk_0"{print substr($0,s,l)}' file 
  • NR==1 if it's the first line (the header line).
  • for(i=1;i<NF;i++) loop trough the fields.
  • $i=="OS_NATIVE_NAME" if the field value equals OS_NATIVE_NAME.
  • s=index(...) find the position of the start of the field and save it for later.
  • l=index(...)-s and the length of the field and save it for later.
  • $1=="disk_0" find the DEVICE you are searching in the first field (disk_0 in the example).
  • {print substr($0,s,l)} finally print for each line, the string started at position s, with length l

Prints (regardless of where the field is in the input):

sda 

With awk:

awk 'NR==1{for(i=1;i<NF;i++){if($i=="OS_NATIVE_NAME"){s=index($0,$i); l=index($0,$(i+1))-s}}} $1=="disk_0"{print substr($0,s,l)}' file 
  • NR==1 if it's the first line (the header line), NR is awks internal variable for the current line number being processed.
  • for(i=1;i<NF;i++) loop trough the fields. NF is awks internal variable for the number of fields in the current line.
  • $i=="OS_NATIVE_NAME" since, we're looping trough each field, check if the field value equals OS_NATIVE_NAME
  • s=index(...) find the position of the start of the field and save it for later. index() is awks string function to get the position of the occurence of a string (here the value of $i, hence OS_NATIVE_NAME) in another string (here $0, hence the whole line).
  • l=index(...)-s get the length of the field and save it for later: same priciple as before, but to get the length we must substract s from it.
  • $1=="disk_0" find the DEVICE you are searching in the first field (disk_0 in the example). $1 represents the first field.
  • {print substr($0,s,l)} finally print for each line, the string started at position s, with length l. substr() is awks string function to cut a string (here $0; the whole line) from position s with length l (the two varibale we prevously extracted while processing line 1)

Prints (regardless of where the field is in the input):

sda 
edited body
Source Link
chaos
  • 49.4k
  • 11
  • 128
  • 147

With awk:

awk 'NR==1{for(i=1;i<NF;i++){if($i=="OS_NATIVE_NAME"){s=index($0,$i); l=index($0,$(i+1))-s}}} $1=="disk_0"{print substr($0,s,l)}' file 
  • NR==1 if it's the first line (the header line).
  • for(i=1;i<NF;i++) loop trough the fields.
  • $i=="OS_NATIVE_NAME" if the field value equals OS_NATIVE_NAME.
  • s=index(...) find the position of the start of the field and save it for later.
  • l=index(...)-s and the length of the field and save it for later.
  • $1=="disk_0" find the DEVICE you are searching in the first field (disk_0 in the example).
  • {print substr($0,s,el)} finally print for each line, the string started at position s, with length l

Prints (regardless of where the field is in the input):

sda 

With awk:

awk 'NR==1{for(i=1;i<NF;i++){if($i=="OS_NATIVE_NAME"){s=index($0,$i); l=index($0,$(i+1))-s}}} $1=="disk_0"{print substr($0,s,l)}' file 
  • NR==1 if it's the first line (the header line).
  • for(i=1;i<NF;i++) loop trough the fields.
  • $i=="OS_NATIVE_NAME" if the field value equals OS_NATIVE_NAME.
  • s=index(...) find the position of the start of the field and save it for later.
  • l=index(...)-s and the length of the field and save it for later.
  • $1=="disk_0" find the DEVICE you are searching in the first field (disk_0 in the example).
  • {print substr($0,s,e)} finally print for each line, the string started at position s, with length l

Prints (regardless of where the field is in the input):

sda 

With awk:

awk 'NR==1{for(i=1;i<NF;i++){if($i=="OS_NATIVE_NAME"){s=index($0,$i); l=index($0,$(i+1))-s}}} $1=="disk_0"{print substr($0,s,l)}' file 
  • NR==1 if it's the first line (the header line).
  • for(i=1;i<NF;i++) loop trough the fields.
  • $i=="OS_NATIVE_NAME" if the field value equals OS_NATIVE_NAME.
  • s=index(...) find the position of the start of the field and save it for later.
  • l=index(...)-s and the length of the field and save it for later.
  • $1=="disk_0" find the DEVICE you are searching in the first field (disk_0 in the example).
  • {print substr($0,s,l)} finally print for each line, the string started at position s, with length l

Prints (regardless of where the field is in the input):

sda 
Source Link
chaos
  • 49.4k
  • 11
  • 128
  • 147

With awk:

awk 'NR==1{for(i=1;i<NF;i++){if($i=="OS_NATIVE_NAME"){s=index($0,$i); l=index($0,$(i+1))-s}}} $1=="disk_0"{print substr($0,s,l)}' file 
  • NR==1 if it's the first line (the header line).
  • for(i=1;i<NF;i++) loop trough the fields.
  • $i=="OS_NATIVE_NAME" if the field value equals OS_NATIVE_NAME.
  • s=index(...) find the position of the start of the field and save it for later.
  • l=index(...)-s and the length of the field and save it for later.
  • $1=="disk_0" find the DEVICE you are searching in the first field (disk_0 in the example).
  • {print substr($0,s,e)} finally print for each line, the string started at position s, with length l

Prints (regardless of where the field is in the input):

sda