How to Delete the String Pattern from a File Between 2 Colon Character

Searched a lot lot but didn't find any solution.

for example: if string is

a12345:hello world:hello world again:other string

output:

a12345::hello world again:other string

so I just wanted to delete the content between 1st and 2nd colon character

Any help would be appreciated.

Thanks,

2 Answers

Use sed:

sed 's/:[^:]*/:/'
  • first : - match colon
  • [^:]* - match any number of characters which are not a colon
  • last : - replace the match with colon, because your match includes the first colon, but from your output I see you want to keep it.
0

Using sed:

sed -i 's/:[^:]*/:/' file_name

Using awk:

awk -F':' -v OFS=":" '{$2="";print}' file_name
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest