• I can't cut fields from "fdisk -l"

    From Ottavio Caruso@21:1/5 to All on Fri Mar 18 12:18:52 2022
    Hi,


    $ sudo fdisk -l
    Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
    Disk model: HITACHI HTS72505
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disklabel type: gpt
    Disk identifier: 6EC2F34F-F0E0-44EB-AF31-94969532B3A5

    Device Start End Sectors Size Type
    /dev/sda1 2048 1023999 1021952 499M Windows recovery environment /dev/sda2 1024000 1226751 202752 99M EFI System
    /dev/sda3 1226752 1259519 32768 16M Microsoft reserved
    /dev/sda4 1259520 103657471 102397952 48.8G Microsoft basic data
    /dev/sda5 103657472 206057471 102400000 48.8G Linux filesystem
    /dev/sda6 223012864 877277183 654264320 312G Microsoft basic data
    /dev/sda7 206057472 223012863 16955392 8.1G Linux swap
    /dev/sda8 877277184 976773119 99495936 47.5G Linux filesystem

    Partition table entries are not in disk order.




    $ sudo fdisk -l |tail -n 10|head -n 8
    /dev/sda1 2048 1023999 1021952 499M Windows recovery environment /dev/sda2 1024000 1226751 202752 99M EFI System
    /dev/sda3 1226752 1259519 32768 16M Microsoft reserved
    /dev/sda4 1259520 103657471 102397952 48.8G Microsoft basic data
    /dev/sda5 103657472 206057471 102400000 48.8G Linux filesystem
    /dev/sda6 223012864 877277183 654264320 312G Microsoft basic data
    /dev/sda7 206057472 223012863 16955392 8.1G Linux swap
    /dev/sda8 877277184 976773119 99495936 47.5G Linux filesystem


    So far, so good.


    Now, I only want fields 1 (Device), 5 (size) and 6 (Type):


    $ sudo fdisk -l |tail -n 10|head -n 8|cut -f 1,5,6 -d" "
    /dev/sda1
    /dev/sda2 1024000
    /dev/sda3 1226752
    /dev/sda4 1259520 103657471
    /dev/sda5 102400000 48.8G
    /dev/sda6 654264320
    /dev/sda7 16955392
    /dev/sda8 99495936


    What am I doing wrong?



    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kenny McCormack@21:1/5 to ottavio2006-usenet2012@yahoo.com on Fri Mar 18 12:55:50 2022
    In article <t11tbd$qh3$1@dont-email.me>,
    Ottavio Caruso <ottavio2006-usenet2012@yahoo.com> wrote:
    ...
    $ sudo fdisk -l |tail -n 10|head -n 8|cut -f 1,5,6 -d" "
    /dev/sda1
    /dev/sda2 1024000
    /dev/sda3 1226752
    /dev/sda4 1259520 103657471
    /dev/sda5 102400000 48.8G
    /dev/sda6 654264320
    /dev/sda7 16955392
    /dev/sda8 99495936

    I couldn't make heads nor tails out of your proposal, but here's how I'd do
    it:

    # fdisk -l | gawk '/^\/dev\// { print $1,$5,$6 }'

    What am I doing wrong?

    Everything.

    P.S. Nitpickers note: Some genius will want to write in to tell me that I
    don't need gawk - that any old AWK will do. This is true, but irrelevant,
    for a half-dozen or so reasons. So, don't bother.

    --
    Men rarely (if ever) manage to dream up a God superior to themselves.
    Most Gods have the manners and morals of a spoiled child.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Janis Papanagnou@21:1/5 to Ottavio Caruso on Fri Mar 18 13:49:49 2022
    On 18.03.2022 13:18, Ottavio Caruso wrote:
    Hi,


    $ sudo fdisk -l
    Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
    Disk model: HITACHI HTS72505
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disklabel type: gpt
    Disk identifier: 6EC2F34F-F0E0-44EB-AF31-94969532B3A5

    Device Start End Sectors Size Type
    /dev/sda1 2048 1023999 1021952 499M Windows recovery environment /dev/sda2 1024000 1226751 202752 99M EFI System
    /dev/sda3 1226752 1259519 32768 16M Microsoft reserved
    /dev/sda4 1259520 103657471 102397952 48.8G Microsoft basic data
    /dev/sda5 103657472 206057471 102400000 48.8G Linux filesystem
    /dev/sda6 223012864 877277183 654264320 312G Microsoft basic data
    /dev/sda7 206057472 223012863 16955392 8.1G Linux swap
    /dev/sda8 877277184 976773119 99495936 47.5G Linux filesystem

    Partition table entries are not in disk order.




    $ sudo fdisk -l |tail -n 10|head -n 8
    /dev/sda1 2048 1023999 1021952 499M Windows recovery environment /dev/sda2 1024000 1226751 202752 99M EFI System
    /dev/sda3 1226752 1259519 32768 16M Microsoft reserved
    /dev/sda4 1259520 103657471 102397952 48.8G Microsoft basic data
    /dev/sda5 103657472 206057471 102400000 48.8G Linux filesystem
    /dev/sda6 223012864 877277183 654264320 312G Microsoft basic data
    /dev/sda7 206057472 223012863 16955392 8.1G Linux swap
    /dev/sda8 877277184 976773119 99495936 47.5G Linux filesystem


    So far, so good.


    Now, I only want fields 1 (Device), 5 (size) and 6 (Type):


    $ sudo fdisk -l |tail -n 10|head -n 8|cut -f 1,5,6 -d" "
    /dev/sda1
    /dev/sda2 1024000
    /dev/sda3 1226752
    /dev/sda4 1259520 103657471
    /dev/sda5 102400000 48.8G
    /dev/sda6 654264320
    /dev/sda7 16955392
    /dev/sda8 99495936


    What am I doing wrong?

    You are using the wrong tool; ignore cut, use awk.

    awk '{print $1,$5,$6}'


    Janis

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From hymie!@21:1/5 to who on Fri Mar 18 12:46:31 2022
    In our last episode, the evil Dr. Lacto had captured our hero,
    Ottavio Caruso <ottavio2006-usenet2012@yahoo.com>, who said:

    $ sudo fdisk -l |tail -n 10|head -n 8
    /dev/sda1 2048 1023999 1021952 499M Windows recovery environment

    Notice that there are multiple spaces between /dev/sda1 and 2048

    $ sudo fdisk -l |tail -n 10|head -n 8|cut -f 1,5,6 -d" "
    /dev/sda1

    cut sees each of those spaces in the above line as a delimiter for an
    empty field

    What am I doing wrong?

    You need something like

    $ sudo fdisk -l |tail -n 10|head -n 8| \
    perl -ane 'print join(" ",@F[0,4,5]),"\n";'

    I'm sure awk will do it, but I don't know awk as well as I know perl.
    And I'm not sure my perl is correct either.

    --hymie! http://nasalinux.net/~hymie hymie@nasalinux.net

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Wayne@21:1/5 to Ottavio Caruso on Sat Mar 19 04:04:05 2022
    On 3/18/2022 8:18 AM, Ottavio Caruso wrote:
    Hi,


    $ sudo fdisk -l
    Disk /dev/sda: 465.8 GiB, 500107862016 bytes, 976773168 sectors
    Disk model: HITACHI HTS72505
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 4096 bytes
    I/O size (minimum/optimal): 4096 bytes / 4096 bytes
    Disklabel type: gpt
    Disk identifier: 6EC2F34F-F0E0-44EB-AF31-94969532B3A5

    Device         Start       End   Sectors  Size Type /dev/sda1       2048   1023999   1021952  499M Windows recovery environment
    /dev/sda2    1024000   1226751    202752   99M EFI System /dev/sda3    1226752   1259519     32768   16M Microsoft reserved /dev/sda4    1259520 103657471 102397952 48.8G Microsoft basic data /dev/sda5  103657472 206057471 102400000 48.8G Linux filesystem
    /dev/sda6  223012864 877277183 654264320  312G Microsoft basic data /dev/sda7  206057472 223012863  16955392  8.1G Linux swap
    /dev/sda8  877277184 976773119  99495936 47.5G Linux filesystem

    Partition table entries are not in disk order.




    $ sudo fdisk -l |tail -n 10|head -n 8
    /dev/sda1       2048   1023999   1021952  499M Windows recovery environment
    /dev/sda2    1024000   1226751    202752   99M EFI System /dev/sda3    1226752   1259519     32768   16M Microsoft reserved /dev/sda4    1259520 103657471 102397952 48.8G Microsoft basic data /dev/sda5  103657472 206057471 102400000 48.8G Linux filesystem
    /dev/sda6  223012864 877277183 654264320  312G Microsoft basic data /dev/sda7  206057472 223012863  16955392  8.1G Linux swap
    /dev/sda8  877277184 976773119  99495936 47.5G Linux filesystem


    So far, so good.


    Now, I only want fields 1 (Device), 5 (size) and 6 (Type):


    $ sudo fdisk -l |tail -n 10|head -n 8|cut -f 1,5,6 -d" "
    /dev/sda1
    /dev/sda2 1024000
    /dev/sda3 1226752
    /dev/sda4 1259520 103657471
    /dev/sda5 102400000 48.8G
    /dev/sda6 654264320
    /dev/sda7  16955392
    /dev/sda8  99495936


    What am I doing wrong?



    The awk solution is probably best, but here's another solution:

    $ sudo fdisk -l |tail -n 10|head -n 8 |tr -s ' ' |cut -f 1,5,6 -d" "

    The tr command there squeezes runs of spaces into a single space.

    --
    Wayne

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)