Results 1 to 3 of 3

Thread: Pull hard disks from /proc/partitions using awk one-liner?

  1. #1

    Default Pull hard disks from /proc/partitions using awk one-liner?

    I'm trying to extract the name of the hard disks with an awk one-liner (for the sake of monitoring / alarming) and am having some trouble getting it to do exactly what I want. I thought maybe one of you might be an awk guru and could help me out.

    So what I'm trying to do is take /proc/partitions as input and pull unique hard disk devices from it.

    Example:

    /proc/partitions:
    Code:
    major minor  #blocks  name
    
       8        0  244198584 sda
       8        1      40131 sda1
       8        2  131516416 sda2
       8        3  104862720 sda3
       8        4          1 sda4
       8        5    7767040 sda5
      11        0    1048575 sr0
       8       16 2930266584 sdb
    I want something like this returned:
    Code:
    sda
    sdb
    sr0
    The current awk line I have is as follows:
    Code:
    awk '{ sub(/[0-9]/,"",$NF); }; { print $NF }' /proc/partitions
    It doesn't take into account the first two lines are content I don't even want to process, nor does it exclude duplicate values (i.e. sda and sda)

    I'm not stuck on awk, but it seems like the most elegant (and shortest command) that will get me the results I'm looking for.

    If you are wondering why I'm targeting /proc/partitions, it is because I'm trying to make this as distro-agnostic and age independent as possible.

    Help, thoughts or ideas are all welcome and appreciated.

  2. #2
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    26,650
    Blog Entries
    15

    Default Re: Pull hard disks from /proc/partitions using awk one-liner?

    Hi
    What about lsblk?
    Code:
    /usr/bin/lsblk --output NAME,TYPE|/usr/bin/awk '/disk|rom/'
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  3. #3
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    26,650
    Blog Entries
    15

    Default Re: Pull hard disks from /proc/partitions using awk one-liner?

    Hi
    This will only show devices...
    Code:
    lsblk --output NAME,TYPE|awk '/disk|rom/ {print substr ($ 3)}'
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

Posting Permissions

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