So I've got a line that looks like this ish


And I want to replace the second : with a tab character. I can reliably match the substring I want to replace with the following:


I want to replace that substring I have there. So I've tried doing some sed

$ echo somelongbullshit:888:morelongbullshitthatcanhaveanything | sed 's/:[0-9]*(:)/\1\t/g'
sed: -e expression #1, char 19: invalid reference \1 on `s' command's RHS

But I get an error. I'm sure there's some syntax error I've got wrong but for the life of me I can find it.

I'm also open to better solutions (like awk).


You can use sed like this with --regex-extended switch:

echo somelongbullshit:888:morelongbullshitthatcanhaveanything | sed --regexp-extended 's/:[0-9]*(:)/\1\t/g'
| improve this answer | |
  • 1
    This does it! Also to keep the numbers I did the following sed --regexp-extended 's/:([0-9]*)(:)/\1\t/g – Christopher Apr 10 '16 at 0:41
  • Mahdi, The present solution has parenthesis in the wrong place... (please see Cristogher comment). – user216043 Jul 14 '16 at 16:01
  1. Not all versions of sed will recognize the tab. so press Ctrl+V and press tab key from your keyboard.

  2. use this command

     $ echo "somelongbullshit:888:morelongbullshitthatcanhaveanything" | sed 's/\:/      /2'
     somelongbullshit:888    morelongbullshitthatcanhaveanything
| improve this answer | |

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.