【问题标题】:Handling inconsistent output in Bash在 Bash 中处理不一致的输出
【发布时间】:2020-11-04 09:53:36
【问题描述】:

如果 Bash 输出并不总是一致,并且某些字段如下所示“不可用”,那么在给定列中获取所有结果的最佳方法是什么?

Slot        Serial          Device           State         Link                UUID     

Slot01      SERIAL_1        /dev/sda        Status_1                    uuid-uuid-uuid-uuid 

Slot02                                      Status_2

Slot03                                      Status_1    /mountpoint3/

Slot04      SERIAL_4        /dev/sdd        Status_4    /mountpoint4/   uuid-uuid-uuid-uuid 

Slot05                                      Status_4    /mountpoint5/

【问题讨论】:

    标签: bash awk sed cut


    【解决方案1】:

    cut -c 允许您指定列数:

    $ cut -c '1-12' < input.txt
    Slot01
    Slot02
    
    $ cut -c '13-28' < input.txt
    SERIAL_1
    

    不要忘记修剪字符串并检查它们是否为空以消除尾随空格并检查空值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 2013-12-28
      相关资源
      最近更新 更多