How to select a particular column in linux df command

前端 未结 2 1716
生来不讨喜
生来不讨喜 2020-12-06 07:50

I am trying to collect information on the amount of space left on different servers. So when I execute df -k i get output as:

Filesystem  1024-         


        
相关标签:
2条回答
  • 2020-12-06 08:29

    fedorqui's solution is cleaner, but only works for df

    A more general approach is to collapse multiple spaces to a single space using sed 's/ \+/ /g' or tr -s ' ', then use cut with spaces as delimiters: cut -d" " -f 4 So the command is:

    df -k | tr -s ' ' | cut -d" " -f 4
    

    Which results in something like this:

    Available
    26027952
    854220
    68376208
    

    The same approach will work with other commands that output data to columns. For instance ls -l | tr -s ' ' | cut -d" " -f 6,7,8 will print the time columns from ls -l:

    Dec 30 17:46
    Mar 1 15:33
    Mar 1 14:58
    Mar 2 00:00
    Jan 5 14:20
    Mar 1 15:33
    Feb 26 11:57
    Feb 4 11:11
    Mar 1 14:57
    
    0 讨论(0)
  • 2020-12-06 08:37

    You can for example say:

    df --output=source,avail
    

    Or as commented by Tim Bunce, you can use --direct to prevent the long filesystem name make the line split in two. This will show the filesystem as -.

    From man df:

    --output[=FIELD_LIST]

    use the output format defined by FIELD_LIST, or print all fields if FIELD_LIST is omitted.

    ...

    FIELD_LIST is a comma-separated list of columns to be included. Valid field names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent', 'size', 'used', 'avail', 'pcent' and 'target' (see info page).

    --direct

    show statistics for a file instead of mount point

    Test

    $ df --output=source,avail
    Filesystem               Avail
    /dev/sda7            321675536
    
    0 讨论(0)
提交回复
热议问题