Find the answer to your Linux question:
Results 1 to 5 of 5
Hello world- I think I need to learn AWK. I am trying to pull simple info from a command to be stored as a variable. I need to get the ...
  1. #1
    Just Joined!
    Join Date
    May 2007
    Posts
    15

    GREP or AWK assistance

    Hello world-

    I think I need to learn AWK. I am trying to pull simple info from a command to be stored as a variable.

    I need to get the /dev/disk? and mount point for a particular drive.

    I'm using:
    Code:
    diskutil info /Volumes/'Macintosh HD' | grep 'Part Of Whole" | cut -c30-34
    and
    Code:
    diskutil info /Volumes/'Macintosh HD' | grep 'Mount Point" | cut -c30-44
    But I'm running into big problems for obvious reasons. The volume information changes, the device number may be higher than 9 or the data is just unusually long. Its not a clean method.

    How do I get the information I need form the standard output without using GREP and CUT? There has to be a better way, I just can't find it.

    Any help would be greatly appreciated.

    Thanks in advance!

    - robbie -

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    It might be a good idea to include information about the output you get and what exactly you need to extract. Not everyone uses a Mac, you know.

  3. #3
    Just Joined!
    Join Date
    May 2007
    Posts
    15
    Sorry for the confusion. I'm a rookie and live in a Mac world.

    Its more a general question. I have no idea how to use AWK or other advanced tools. I'm trying but is not going so well. I could get similar information from hdiutil or other commands - diskutil is just this example.

    I'm trying to find out how to best get the info I need.

    The output I get is:

    Code:
    administrator:~ administrator$ diskutil info /Volumes/Untitled 
       Device Identifier:        disk2s2
       Device Node:              /dev/disk2s2
       Part Of Whole:            disk2
       Device / Media Name:      Untitled
    
       Volume Name:              Untitled
       Mount Point:              /Volumes/Untitled
       File System:              Journaled HFS+
                                 Journal size 16384 KB at offset 0x189c000
       Owners:                   Disabled
    
       Partition Type:           Apple_HFS
       Bootable:                 Is bootable
       Media Type:               Generic
       Protocol:                 FireWire
       SMART Status:             Not Supported
       Volume UUID:              36AFDD45-AB5B-392D-BC32-0D3080618B28
    
       Total Size:               148.7 Gi (159697911808 B) (311909984 512-byte blocks)
       Free Space:               148.7 Gi (159666061312 B) (311847776 512-byte blocks)
    
       Read Only:                No
       Ejectable:                Yes
       Whole:                    No
       Internal:                 No
    I would like to know how to get just the "disk2" from "Part Of Whole:".

    I can grep and cut - but if I'm dealing with drive #10 then I'm in trouble because my grep and cut would leave me with disk1 because I have to cut the line without the spaces.

    I use:

    Code:
    administrator:~ administrator$ diskutil info /Volumes/Untitled | grep 'Part Of Whole:' | cut -c30-34
    disk2
    So I get what I want but only if I know exactly how many characters it is.

    I'm not sure what I need to do. Thats why I'm throwing it out to the people that know way more than I do.


    As always if anyone can enlighten me I would appreciate it. Thanks in advance!

    - robbie -

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    try
    Code:
    diskutil info /Volumes/Untitled | awk 'BEGIN{FS="[: ]+"}/Part Of Whole/{print $5}'
    If you really want to make cut to work, try
    Code:
    diskutil info /Volumes/Untitled | grep "Part Of Whole" | cut -d ':' -f 2 | tr -d ' '
    I prefer the awk approach because it requires only on command after the first pipe.

  5. #5
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    In a Linux programming forum, you should not expect anyone to be acquainted with a Mac-specific program like "diskutil". You could also use sed:

    Code:
    diskutil info /Volumes/Untitled | sed -n 's/[\t ]*Part Of Whole:[\t ]*\([[:alnum:]]\+\)/\1/p'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...