3

I have a log file with lines like this:

06:06:48.531 9: 157 2492
06:06:49.331 10: 147 2812
...

I need to remove only the 3rd ':' on all lines, so that the outcome would be:

06:06:48.531 9 157 2492
06:06:49.331 10 147 2812
...
  • 7
    sed -e 's/://3' – cuonglm Nov 1 '15 at 11:34
  • i'd wrong idea about the number in the end of command (3) , i though it the total replaced characters , but it's the Nth character , +1 cuonglm :) – Jonah Nov 1 '15 at 11:51
  • If you change the question, you should begin a new question. – Thomas Dickey Nov 1 '15 at 11:57
  • @cuonglm, if you could post an Answer with your comment above I will rate it as this is for me the most elegant solution! – fduff Feb 18 '16 at 15:52
4

POSIXLY:

sed -e 's/://3' <file
1

the 3rd ':' is followed by space , which make it different to others !

sed -i 's/: / /g' logfile
  • The g is not necessary. If there are several :␣, then they would all get removed... – Kusalananda Oct 11 '17 at 12:16
1

An awk solution:

awk '{ print(gensub(/:/,"",3,$0)) }' logfile
1

The long version (using a method which can be very useful when one gets tired and doesn't want to think too much...):

sed -e 's/\([^:]*\):\([^:]*\):\([^:]*\):/\1:\2:\3/'

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.