regex help on unix df

前端 未结 7 1927
耶瑟儿~
耶瑟儿~ 2020-12-02 01:22

I need some help tweaking my code to look for another attribute in this unix df output:

Ex.

Filesystem     Size    Used   Avail Capacity         


        
7条回答
  •  青春惊慌失措
    2020-12-02 01:46

    Lots of variations on a theme here. I would keep the first line, since it gives a nice header:

    $ perl -E '$,=" "; open my $fh, "-|", "df -k /tmp"; 
      while(<$fh>) { @a=split; say @a[3,4]}'
    

    On second thought, this is a lot cleaner:

    $ df -k /tmp | perl -naE '$,="\t"; say @F[3,4]'
    Available       Capacity
    20862392        92%
    

    Final thought: don't use perl at all:

    $ df -h /tmp | tr -s ' ' '\t'  | cut  -f 3,4
    

    or

    $ df -h /tmp | awk '{print $3 "\t" $4}'
    

提交回复
热议问题