Home > Software design >  How to remove anything enclosed in : from a text?
How to remove anything enclosed in : from a text?

Time:02-04

I have a text which includes multiple substrings like :xxx: which should be removed using awk. The xxx-part could be any string but cannot contain white spaces or line breaks.

Here is an example text:

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

The command should turn that text into:

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

My pathetic attempt:

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

CodePudding user response:

Using sed

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

Using awk

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