Logo Questions Linux Laravel Mysql Ubuntu Git Menu

sed inserting dash into string

I have 14-character line containing digits. How do I insert a char into it at the specific location, i.e. at 4th? So, if I have string like this: xxxxxxxxxxxxxx how do I change it to something like this: xxxx-xx-xxxxxxxx ? (x = digit)

Thanks! irek

like image 742
irek Avatar asked Feb 24 '23 09:02


1 Answers

If your lines only contain your digits, you can group the first four characters in a group:


and the following two ones in another group:


Then, you just replace it by a backreference to the first group (\1), a dash, a backreference to the second group (\2) and another dash:


The result:

$ echo 12345678900000 | sed 's/\(....\)\(..\)/\1-\2-/'
like image 183
brandizzi Avatar answered Mar 04 '23 13:03