0

I'm try to extract the contents of a given column, but spaces have been used to format the columns so I don't have a specific delimiter to work with.

The data I'm working with is the result of the following OS X command:

diskutil list | grep 'Apple'

Example output:

   2:                  Apple_HFS Macintosh SSD           511.8 GB   disk0s2
   2:                  Apple_HFS External                79.7 GB    disk1s2

The column I want to extract is the disk name, i.e. "Macintosh SSD" and "External"

0

3 Answers 3

4

You should use diskutil list -plist and parse the property list instead. The human-readable output is not meant to be machine readable, and might look different than you expect (column boundaries could move to accommodate long outputs or a wide terminal window, etc).

Sign up to request clarification or add additional context in comments.

2 Comments

Like @ "The human-readable output is not meant to be machine readable"
Thank you. It wasn't the answer I was hoping for, but it's spot on.
0

If I understand correctly, you need to print the words from the 3rd to the Nth-3. If so, the following awk will do:

gawk '{disk=""; for (i = 3; i <= NF-3; i++){disk=disk $i " "};print disk}'

Comments

0

You could also use tr -s ' ' to collapse (squeeze) spaces:

diskutil list | tr -s ' ' | cut -d ' ' -f 3 | grep Apple

Or just regex:

diskutil list | sed -En 's/^[ 0-9]+: +(Apple[^ ]+).+/\1/p'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.