1

I have a file that has below entries:

[root@ovmm329 ovm_utils]# cat /tmp/disk.txt
Physical Disk : 'IBM (67) device=/dev/mapper/360050768018206483000000000000453 shareddisk=true
Physical Disk : 'IBM (58) device=/dev/mapper/36005076801820648300000000000043f shareddisk=true
Physical Disk : 'IBM (59) device=/dev/mapper/360050768018206483000000000000440 shareddisk=true
Physical Disk : 'IBM (71) device=/dev/mapper/360050768018206483000000000000497 shareddisk=true
[root@ovmm329 ovm_utils]#

I tried using sed and awk in all possible way which I am aware of but couldn't get it in the required format. I would like to have the file in this format:

Physical Disk  device                                          Shareddisk
IBM (67)        /dev/mapper/360050768018206483000000000000453  true
IBM (58)        /dev/mapper/36005076801820648300000000000043f  true
IBM (59)        /dev/mapper/360050768018206483000000000000440  true
IBM (71)        /dev/mapper/360050768018206483000000000000497  true
  • Does this: awk -F [:\'] '{print $3}' infile solve your need? – user79743 Jan 27 '16 at 19:15
  • The format of disk.txt is changing. Is the current value correct? – jecxjo Jan 27 '16 at 19:33
  • Thank you for the help Binary and Jecxjo your suggestion worked and helped me to get the data in the required format. – IgniteLX Jan 28 '16 at 14:27
  • Binary, awk -F [:\'] '{print $3}' infile this didn't helped but i believe this will be helpful for me in future. – IgniteLX Jan 28 '16 at 14:28
2

Using awk variables and multiple matches you can read each line, store what you need and then print on the last match and start over.

#!/usr/bin/awk -f

BEGIN {
  DISK=""
  DEVICE=""
  FS=" : "
  print "Physical Disk\tdevice\t\t\t\t\t\tShareddisk"
  }

/Physical Disk/ {
  DISK=$2
  FS="="
}

/device/ {
  DEVICE=$2
}

/shareddisk/ {
  print DISK "\t" DEVICE "\t" $2
  FS=" : "
  }

Then run the script against your file awk -f ./script.awk ./disk.txt

$ awk -f ./parse.awk ./disk.txt
Physical Disk   device                                          Shareddisk
'IBM (67)       /dev/mapper/360050768018206483000000000000453   true
'IBM (58)       /dev/mapper/36005076801820648300000000000043f   true
'IBM (59)       /dev/mapper/360050768018206483000000000000440   true
'IBM (71)       /dev/mapper/360050768018206483000000000000497   true

To break down the script, it starts with a BEGIN block which is run at the start. It defines some variables and sets the Field Separator FS and prints the header. The first match is for Physical Disk and stores the disk name to a variable. The Field Separator is changed to = since the Device and Shared Disk lines use =. When the line matching /shareddisk/ is hit the variables are printed and the Field Separator is changed again to match the next Physical Disk line.

Edit

This assumes the format of the file is:

cat disk.txt
Physical Disk : 'IBM (67)
  device=/dev/mapper/360050768018206483000000000000453
  shareddisk=true
Physical Disk : 'IBM (58)
  device=/dev/mapper/36005076801820648300000000000043f
  shareddisk=true
Physical Disk : 'IBM (59)
  device=/dev/mapper/360050768018206483000000000000440
  shareddisk=true
Physical Disk : 'IBM (71)
  device=/dev/mapper/360050768018206483000000000000497
  shareddisk=true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.