如何从文本中删除 : 中包含的任何内容?

How to remove anything enclosed in : from a text?

我有一个文本包含多个子字符串,例如 :xxx:,应该使用 awk 将其删除。 xxx 部分可以是任何字符串,但不能包含空格或换行符。

这是一个示例文本:

This is sample text:hello:
It might include normal colons like : or: that have to remain. :hello-world: 

该命令应将该文本转换为:

This is sample text 
It might include normal colons like : or: that have to remain.

我可怜的尝试:

awk '{gsub(":.*:","")}1'

使用sed

$ sed 's/:[^ :]*://g' input_file
This is sample text
It might include normal colons like : or: that have to remain.

使用awk

$ awk '{gsub(/:[^ :]*:/,"")}1' input_file
This is sample text
It might include normal colons like : or: that have to remain.

The xxx-part could be any string but cannot contain white spaces or line breaks.

awk 'gsub(/:[^[:space:]]*:/,"") 1' file
This is sample text
It might include normal colons like : or: that have to remain.

对于此要求,您可以使用字符 class [:space:] 作为 space 个字符。

These are: space, TAB, newline, carriage return, formfeed and vertical tab). See "POSIX character classes" here: https://www.gnu.org/software/gawk/manual/html_node/Bracket-Expressions.html