awk help

I have a one liner command I am using in OS X to grab the volume name, but if the user sets spaces in their names it doesn't quite work.

I would like awk to print everything after value $3 in my script, in case they have spaces and multiple names of their boot volume. Can I wild card it, so that awk will print every value after $3?

example of command:

bash-3.2# diskutil info `bless --getboot` | awk '/Volume Name:/ { print $3 }'

Thanks

Can you post the output of:

diskutil info `bless --getboot`

and the desired output?

`bless --getboot` just gets the device it is booting from, so the output would be /dev/disk0s2 on almost all default machines, but in some cases that may not be the case if the desktop has multiple drives and someone imaged the wrong one by accident.

I want to enforce a standard volume name for the boot volume, so that the boot volume is always named Macintosh HD, no matter what. Users are renaming it, and well it breaks a few things myself and others do when they rename it. Some Windows apps we run in cross over API don't like /dev/disk entries they want volume names, so I gotta make sure the boot volume is always named something standard.

Here is the output of the command like you requested:

bash-3.2# diskutil info `bless --getboot`
   Device Identifier:        disk0s2
   Device Node:              /dev/disk0s2
   Part Of Whole:            disk0
   Device / Media Name:      Apple_HFS_Untitled_1

   Volume Name:              OSX HD
   Mount Point:              /
   File System:              Journaled HFS+
                             Journal size 16384 KB at offset 0x427000
   Owners:                   Enabled

   Partition Type:           Apple_HFS
   Bootable:                 Is bootable
   Media Type:               Generic
   Protocol:                 SATA
   SMART Status:             Verified
   Volume UUID:              8F810E00-C368-3A5C-98F0-B3D25F0B54B8

   Total Size:               132.6 Gi (142341193728 B) (278010144 512-byte blocks)
   Free Space:               84.5 Gi (90768429056 B) (177282088 512-byte blocks)

   Read Only:                No
   Ejectable:                No
   Whole:                    No
   Internal:                 Yes

This is my work desktop, which is not managed because I don't need to run any of the apps my users do. However, some users may have changed the boot volume name to like "BLING-BLING," or "MAC DADDY," and that is not a joke. I need to be able to script it to always detect the volume name and rename if possible. So my desired output would be to grab the whole volume name, regardless of how many words, or spaces it has. I was hoping to wild card the print $3 option to grab everything after that in my awk command.

Now that I think about it, I guess I could just always force a volume name change, but at this point in time I was looking to expand my awk skills, heh. :slight_smile:

Can you color the output that you are looking for?

It is in red. I need the full volume name, with all words and spaces. Thanks

Not shure what you mean with "with all words and spaces", but have a choice:

The line without trimming the spaces:

awk '/Volume Name:/' 

Trimmed:

awk '/Volume Name:/ {$1=$1;print}' 

Only the OS:

awk '/Volume Name:/ {sub(".*  ","");print}' 

My boot volume is named OSX HD, so it is two words with a space in the middle. It could be different on every machine as the end user is able to rename it whatever they want. So, I need to some how wild card awk so it prints the whole volume name.

Sorry if I wasn't clear before, but does that make sense? So my desired output would be OSX HD on the info given, but if a user names their hard drive say Mac hard drive, that would be three words, with two spaces, so I need to capture the whole value(s) after /Volume Name:/.

I hope I explained it properly this time

maybe with grep and sed combination:

grep -i "volume name" infile | sed 's/\(Volume Name:[ ]*\) \(.*\)/\2/' 

Maybe something like this?

sed -n '/Volume Name:/ s/.*Volume Name: *//p' 

Awesome guys, my sed wizardry is not so great, so I was going with awk since I know it a bit better.

Franklin - yours does it, thanks you rock!!!!

Eagle - I get a blank response, it displays nothing....not sure why.

This forum is awesome thanks for helping me, now if you can explain what the sed command is doing, /s excludes something from the output correct?

sed -n 's/.*Volume Name: *//p' file
sed -n '/.*Volume Name: */s///p' file

Frankly Franklin i coudlnt remember how to put this part

/Volume Name:/

into my sed code not to use grep :smiley:

thanks

sed -n '/Volume Name:/ s/.*Volume Name: *//p'

Explanation:

-n			# Don't print the lines per default
/Volume Name:/ 		# Search for ...(not required as anbu23 mentioned)
s/			# Substitute command
.*Volume Name:		# Substitute until ".*Volume Name:"
 *			# ++ and one or more spaces
//			# ++ with nothing
p'			# Print the line

Thanks, I bought the O'Reilly sed and awk bible but I haven't got a chance to really dig into it. Still learning the basics of awk, and man it makes scripting so much easier with those two commands.

Thanks for everyone's input.