3

I am trying to create a command that works with the output to check the partition type and name. I have just discovered a solution using awk or perhaps sfdisk?

Here's my base output:

root@debian:/home/si# sudo sfdisk -l /dev/sda --quiet | awk '{print $0}'
Périphérique Amorçage   Début      Fin Secteurs Taille Id Type     
/dev/sda1    *           2048  2000895  1998848   976M 83 Linux    
/dev/sda2             2000896 86962175 84961280  40,5G 8e LVM Linux

Now let's filter on column $1 ,$7 and $8

root@debian:/home/si# sudo sfdisk -l /dev/sda --quiet | awk '{print $1, $7, $8}'
Périphérique Id Type  
/dev/sda1 83 Linux    
/dev/sda2 LVM Linux  

It seems there's an offset on column because LVM value on /dev/sda2 should belong to the Type column, so the $8 column if I'm right.

How does awk displays the columns ? Is there any filter I miss ?

In my mind, the output columns display should be like (| manually add)

$1           |      $2  |     $3   |   $4     |    $5    |   $6   | $7 | $8
Périphérique | Amorçage |  Début   |   Fin    | Secteurs | Taille | Id | Type     
/dev/sda1    |*         |  2048    | 2000895  |  1998848 |  976M  | 83 | Linux    
/dev/sda2    |          |  2000896 | 86962175 |84961280  | 40,5G  | 8e | LVM Linux
2
  • Are your columns really sometimes left-aligned (Périphérique Amorçage Type) and sometimes right-aligned (Début Fin Secteurs Taille)?
    – Ed Morton
    Jun 6 at 17:06
  • I thought it was but it seems not as someone mentionned in the answer below.
    – motorbass
    Jun 6 at 18:50

2 Answers 2

7

Your picture is erroneous. The last line would look like this:

      1      |     2   |    3     |     4    |    5   |  6 |  7  | 8
/dev/sda2    | 2000896 | 86962175 |84961280  | 40,5G  | 8e | LVM | Linux

Because some of your fields contain more whitespace than others, and some lines are missing certain columns, trying to extract data by field number is going to be tricky. Consider using the JSON output available from sfdisk that largely avoid these problems:

sudo sfdisk -J /dev/nvme0n1 |
  jq -r '.partitiontable.partitions[]|[.node, .size, .type]|@tsv'

That would output something like (on my system):

/dev/nvme0n1p1  409600  C12A7328-F81F-11D2-BA4B-00A0C93EC93B
/dev/nvme0n1p2  2097152 0FC63DAF-8483-4772-8E79-3D69D8477DE4
/dev/nvme0n1p3  493076480   E6D6D379-F507-44C2-A23C-238F2A3DF928

You'll notice that this uses UUIDs to identify partition types rather than names, so you may need to map that back to something human readable depending on your goals.

1
  • Hey ! thanks a lot for all of this knowledge. I really thought that awk detect "column header"-like. I wasn't aware it was based on space separator. You absolutely right, in my case the JSON output is far easier, thanks again for this advice !
    – motorbass
    Jun 6 at 18:53
6

The output of sfdisk is formatted by physical columns rather than logical fields.

To parse it with Awk, use GNU Awk. GNU Awk has support for parsing fixed columns.

This works with my installation of sfdisk:

$ sudo sfdisk -l /dev/sda --quiet | gawk -v OFS="|" -v FIELDWIDTHS='11 4 9 9 9 6 3 40'  '$1=$1'
Device     |Boot|    Start|      End|  Sectors|  Size| Id| Type
/dev/sda1  |*   |     2048| 50427903| 50425856|   24G| 83| Linux
/dev/sda2  |    | 50429950| 52426751|  1996802|  975M|  5| Extended
/dev/sda5  |    | 50429952| 52426751|  1996800|  975M| 82| Linux swap / Solaris

Note that the fields are not trimmed of whitespace.

Another trick we can play is to take advantage of the only field being missing is the boot flag *. We can stick some alternative character in its place, like ! and then get Awk to re-parse the record using $1=$1:

$ sudo sfdisk -l /dev/sda --quiet  | awk -v OFS="," 'NR > 1 && $2 != "*" { $0=substr($0, 1, 11) "!" substr($0, 13) } ; $1=$1'
Device,Boot,Start,End,Sectors,Size,Id,Type
/dev/sda1,*,2048,50427903,50425856,24G,83,Linux
/dev/sda2,!,50429950,52426751,1996802,975M,5,Extended
/dev/sda5,!,50429952,52426751,1996800,975M,82,Linux,swap,/,Solaris

We no longer have spurious blanks, but on the downside, the type field gets munged into fields, of course; if it is needed in one piece there are ways.

1
  • Pretty ingenious ! i'll have a look at gawk, thanks a lot !
    – motorbass
    Jun 7 at 6:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .