2

I want to print all disks that are equal or more then 18G
how to fit the following syntax in order to print only the disks that are => 18G

lsblk -l  | awk '/disk/'

fd0            2:0    1     4K  0 disk
sda            8:0    0   150G  0 disk
sdb            8:16   0    20G  0 disk 
sdc            8:32   0    20G  0 disk 
sdd            8:48   0    20G  0 disk 
sde            8:64   0    20G  0 disk 
sdf            8:80   0    20G  0 disk 
sdg            8:96   0    20G  0 disk
sdh            8:112  0    20G  0 disk
sdi            8:128  0    20G  0 disk
sdj            8:144  0    2G   0 disk
sdk            8:160  0    2G   0 disk
2

lsblk + awk + numfmt solution:

lsblk -lnb | awk '$4>=19327352832' | numfmt --to=iec --field=4

lsblk options:

  • -n - do not print a header line
  • -b - print the SIZE column in bytes rather than in a human-readable format

awk details:

  • $4 - the 4th field value(SIZE column)
  • 19327352832 - equivalent to 18G in byte units

numfmt options:

  • --to=iec - auto-scale output numbers to UNITs; accept optional single letter suffix: 1K = 1024, 1M = 1048576, ...
  • --field=<FIELDS> - replace/transform the numbers in these input fields FIELDS
  • how you calculate the value - 19327352832? for 18G ? – yael Dec 24 '17 at 13:31
  • 18 * 1024 = 18432MB; 18432 * 1024 = 18874368KB; 18874368 * 1024 = 19327352832B. Google can't be trusted for byte conversions anymore because the went all metric and to them 1KB = 1000 Bytes and they have added a KiB which is equal to 1024 bytes, but even that system often gives me inaccuracies. – jesse_b Dec 24 '17 at 13:35
  • 2
    @Jesse_b - google is a search engine, "they" don't do any conversions and as long as you don't mistake SI units for IEC units there should be no inaccuracy. – don_crissti Dec 24 '17 at 15:37
  • @don_crissti, fair enough but I still want to rag on google for it because their conversions used to be IEC only and now they default to SI. The wiki you linked says: "There have been many attempts to resolve the confusion by providing alternative notations for power-of-two multiples." But in my opinion every effort to resolve this confusion only adds to it. I don't see a need for the SI standard when referring to data sizes. – jesse_b Dec 24 '17 at 15:45
  • @Jesse_b - that's also very true... – don_crissti Dec 24 '17 at 15:50
0

My lsblk doesn't work, but does this work for you?

lsblk -l | awk '{
    if ($4~/.*G/ && $4/1 >= 18) || 
       ($4~/.*T/)
       print $0;
    }'

#As a single line:
lsblk -l | awk '{if ($4~/.*G/ && $4/1 >= 18) || ($4~/.*T/){print $0}}'

awk program

  • $4~/.*G/ is matching any pattern in field 4 ending in G
  • $4/1 >= 18 if it ends in G, divide it by 1 (to remove the G) and check if it's greater than or equal to 18
  • $4~/.*T/ or if it the string in field 4 ends in T it passes.

Note: RomanPerekhrest's solution is more robust.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.