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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like