9

The following lsblk command print the disk usage in bytes

 lsblk -bio KNAME,TYPE,SIZE,MODEL| grep disk 

 sda   disk  298999349248 AVAGO
 sdb   disk 1998998994944 AVAGO
 sdc   disk 1998998994944 AVAGO
 sdd   disk 1998998994944 AVAGO
 sde   disk   98998994944 AVAGO

how to print the disks when disk is greater than 300000000000 , by adding after the pipe awk or perl one-liner or else

expected output:

 lsblk -bio KNAME,TYPE,SIZE,MODEL| grep disk | ......
 sdb   disk 1998998994944 AVAGO
 sdc   disk 1998998994944 AVAGO
 sdd   disk 1998998994944 AVAGO
2
  • awk '$3 >= 1998998994944' infile Sice your output doesn't have a disk larger than 300000000000 you won't be able to print anything. The syntax will work in general. $3 > some_value then print. Oct 2, 2017 at 8:19
  • I not want to work with files
    – yael
    Oct 2, 2017 at 8:24

3 Answers 3

11

You can do it with awk itself for pattern matching instead of using grep.

lsblk -bio KNAME,TYPE,SIZE,MODEL| awk '/disk/ && $3> 300000000000 || NR==1'

Or use scientific value 3e11.

0
9

Short Awk approach:

lsblk -nbio KNAME,TYPE,SIZE,MODEL | awk '$3>3e11'

  • -n (--noheadings) - don't print headings

  • $3 - the 3rd field (SIZE column)

  • 3e11 - E-notation. the letter E (or e) is often used to represent "times ten raised to the power of" (which would be written as "× 10n") and is followed by the value of the exponent; in other words, for any two real numbers m and n, the usage of "mEn" would indicate a value of m × 10n. 3e11 is equivalent to 300000000000.

1

Perl solution:

lsblk -bio KNAME,TYPE,SIZE,MODEL | perl -ane 'print if $F[2] > 3e11'

You can use 300_000_000_000 as the value also.

  • -n reads the input line by line without printing
  • -a splits the input on whitespace into the @F array

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