【问题标题】:BASH split a string and return all of the text before that wordBASH 拆分一个字符串并返回该单词之前的所有文本
【发布时间】:2020-07-22 01:16:04
【问题描述】:

我有一个字符串,其中包含我需要获取的信息,然后是一个我想删除的警告:

Time Sync Status:       Local time: Mon 2020-04-06 19:25:43 EDT
  Universal time: Mon 2020-04-06 23:25:43 UTC
        RTC time: n/a
       Time zone: America/New_York (EDT, -0400)
 Network time on: no
NTP synchronized: no
 RTC in local TZ: yes


         Warning: This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.

所以我试图拆分“警告”上的字符串并在此之前获取文本......但我已经尝试了很多东西,但没有任何东西对我有用:

echo Time Sync Status: "$(timedatectl status | awk -F 'Warning' '{print $1}')"

我怎样才能做到这一点?

TIA 罗恩

【问题讨论】:

    标签: string bash awk split


    【解决方案1】:

    请您尝试关注一下。

    awk '/Warning/{exit} 1' Input_file
    

    echo "$string" | awk '/Warning/{exit} 1'
    

    解释: 只需寻找单词Warning,一旦出现就退出程序。在此之前它会打印每一行。



    第二个解决方案:如果您想要单词 Warning 之前的所有行并且也想留下空行,请尝试以下操作。

    awk '/Warning/{exit};NF' Input_file
    

    echo "$string" | awk '/Warning/{exit};NF'
    


    第三种解决方案:如果 OP 想要按照他/她的风格做,那么请尝试(在 GNU awk 中测试和编写)。我们需要设置记录分隔符以使 FS 在这里工作。

    awk -v RS="" -v FS="Warning" '{print $1}'  Input_file
    

    echo "$string" | awk -v RS="" -v FS="Warning" '{print $1}'
    

    【讨论】:

      猜你喜欢
      • 2022-06-22
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 2011-08-24
      • 2015-01-21
      • 1970-01-01
      相关资源
      最近更新 更多