简体   繁体   English

如何使用sed命令删除短语后的数字和日期时间?

[英]How to delete numbers and date time after phrase using sed command?

Following syntax delete everything after specific phrase: 以下语法删除特定短语之后的所有内容:

s/\(.*\Build ID:.\).*/\1/g
s/\(.*\Build Date:.\).*/\1/g

However I need to secure this part of code and to delete: 但是,我需要保护这部分代码并删除:

  • only 8 digits number date time after specific phrase 特定词组后仅8位数字的日期时间
  • only date time (YYYY-MM-DD space HH:MM:SS) after specific phrase 特定词组之后的仅日期时间(YYYY-MM-DD空间HH:MM:SS)

Input: 输入:

Build ID: 14257823
Build Date: 2019-06-27 03:09:33

Output: 输出:

Build ID:
Build Date:

To cut only 8-digits and time stamp please use: 要仅削减8位数字和时间戳,请使用:

s/[0-9]\\{8\\}//gs/[0-9]\\{4\\}\\-[0-9]\\{2\\}\\-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}//g

You can use character classes and repetition quantifier. 您可以使用字符类和重复量词。

The build id format in your example is [0-9]\\{8\\} (the character class takes all digits and repeats them 8 times) and the date format is [0-9]\\{4\\}\\-[0-9]\\{2\\}\\-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\} : 您的示例中的构建ID格式为[0-9]\\{8\\} (字符类采用所有数字并将其重复8次),日期格式为[0-9]\\{4\\}\\-[0-9]\\{2\\}\\-[0-9]\\{2\\} [0-9]\\{2\\}:[0-9]\\{2\\}:[0-9]\\{2\\}

  • four digits 四位数
  • hyphen 连字号
  • two digits 两位数
  • hyphen 连字号
  • two digits 两位数
  • space 空间
  • two digits 两位数
  • colon 结肠
  • two digits 两位数
  • colon 结肠
  • two digits 两位数

This gives the following regexes 这给出了以下正则表达式

s/\(.*Build ID:.\)[0-9]\{8\}/\1/g
s/\(.*Build Date:.\)[0-9]\{4\}\-[0-9]\{2\}\-[0-9]\{2\} [0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}/\1/g

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM