print line only if number in third field is greater than X

50,668

Solution 1

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.

Solution 2

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.

Solution 3

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
Share:
50,668

Related videos on Youtube

yael
Author by

yael

Updated on September 18, 2022

Comments

  • yael
    yael over 1 year

    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
    
    • Marek Zakrzewski
      Marek Zakrzewski over 6 years
      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.
    • yael
      yael over 6 years
      I not want to work with files